Skip to content

Commit 72c974f

Browse files
Merge pull request #10 from VH-Lab/refactor-tests-structure-1426338253063092469
Refactor tests to mirror package structure
2 parents b1142fc + 80d3ff7 commit 72c974f

52 files changed

Lines changed: 1598 additions & 1 deletion

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pip install -e .
4646
To run the tests, use the following command:
4747

4848
```bash
49-
python -m unittest discover -s tests/ndi/unittest -t .
49+
python -m unittest discover -s tests -t .
5050
```
5151

5252
### Building Documentation

tests/nditests/__init__.py

Whitespace-only changes.

tests/nditests/unittest/__init__.py

Whitespace-only changes.

tests/nditests/unittest/cloud/__init__.py

Whitespace-only changes.

tests/nditests/unittest/cloud/api/__init__.py

Whitespace-only changes.

tests/nditests/unittest/cloud/api/documents/__init__.py

Whitespace-only changes.
Lines changed: 348 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,348 @@
1+
import unittest
2+
from unittest.mock import patch, Mock
3+
import os
4+
from ndi.cloud.api import url
5+
from ndi.cloud.api.auth.login import login
6+
from ndi.cloud.api.auth.logout import logout
7+
from ndi.cloud.api.auth.change_password import change_password
8+
from ndi.cloud.api.auth.resend_confirmation import resend_confirmation
9+
from ndi.cloud.api.auth.reset_password import reset_password
10+
from ndi.cloud.api.auth.verify_user import verify_user
11+
from ndi.cloud.api.datasets.get_published import get_published
12+
from ndi.cloud.api.datasets.get_unpublished import get_unpublished
13+
from ndi.cloud.api.datasets.list_datasets import list_datasets
14+
15+
class TestCloudApi(unittest.TestCase):
16+
17+
def test_get_url_prod(self):
18+
"""
19+
Tests the get_url function in the production environment.
20+
"""
21+
os.environ['CLOUD_API_ENVIRONMENT'] = 'prod'
22+
test_url = url.get_url('login')
23+
self.assertEqual(test_url, "https://api.ndi-cloud.com/v1/auth/login")
24+
25+
def test_get_url_dev(self):
26+
"""
27+
Tests the get_url function in the development environment.
28+
"""
29+
os.environ['CLOUD_API_ENVIRONMENT'] = 'dev'
30+
test_url = url.get_url('login')
31+
self.assertEqual(test_url, "https://dev-api.ndi-cloud.com/v1/auth/login")
32+
33+
def test_get_url_with_params(self):
34+
"""
35+
Tests the get_url function with path parameters.
36+
"""
37+
os.environ['CLOUD_API_ENVIRONMENT'] = 'prod'
38+
test_url = url.get_url('get_user', user_id='123')
39+
self.assertEqual(test_url, "https://api.ndi-cloud.com/v1/users/123")
40+
41+
def test_get_url_missing_param(self):
42+
"""
43+
Tests that get_url raises a ValueError when a required parameter is missing.
44+
"""
45+
with self.assertRaises(ValueError):
46+
url.get_url('get_user')
47+
48+
@patch('requests.post')
49+
def test_login_success(self, mock_post):
50+
"""
51+
Tests the login function on a successful API call.
52+
"""
53+
mock_response = Mock()
54+
mock_response.status_code = 200
55+
mock_response.json.return_value = {'token': 'fake_token'}
56+
mock_post.return_value = mock_response
57+
58+
success, answer, _, _ = login('test@example.com', 'password')
59+
60+
self.assertTrue(success)
61+
self.assertEqual(answer['token'], 'fake_token')
62+
63+
@patch('requests.post')
64+
def test_login_failure(self, mock_post):
65+
"""
66+
Tests the login function on a failed API call.
67+
"""
68+
mock_response = Mock()
69+
mock_response.status_code = 401
70+
mock_response.json.return_value = {'error': 'Invalid credentials'}
71+
mock_post.return_value = mock_response
72+
73+
success, answer, _, _ = login('test@example.com', 'password')
74+
75+
self.assertFalse(success)
76+
self.assertEqual(answer['error'], 'Invalid credentials')
77+
78+
@patch('ndi.cloud.api.implementation.auth.logout.authenticate')
79+
@patch('requests.post')
80+
def test_logout_success(self, mock_post, mock_authenticate):
81+
"""
82+
Tests the logout function on a successful API call.
83+
"""
84+
mock_authenticate.return_value = 'fake_token'
85+
mock_response = Mock()
86+
mock_response.status_code = 200
87+
mock_response.json.return_value = {'message': 'Logout successful'}
88+
mock_post.return_value = mock_response
89+
90+
success, answer, _, _ = logout()
91+
92+
self.assertTrue(success)
93+
self.assertEqual(answer['message'], 'Logout successful')
94+
95+
@patch('ndi.cloud.api.implementation.auth.logout.authenticate')
96+
@patch('requests.post')
97+
def test_logout_failure(self, mock_post, mock_authenticate):
98+
"""
99+
Tests the logout function on a failed API call.
100+
"""
101+
mock_authenticate.return_value = 'fake_token'
102+
mock_response = Mock()
103+
mock_response.status_code = 401
104+
mock_response.json.return_value = {'error': 'Invalid token'}
105+
mock_post.return_value = mock_response
106+
107+
success, answer, _, _ = logout()
108+
109+
self.assertFalse(success)
110+
self.assertEqual(answer['error'], 'Invalid token')
111+
112+
@patch('ndi.cloud.api.implementation.auth.change_password.authenticate')
113+
@patch('requests.post')
114+
def test_change_password_success(self, mock_post, mock_authenticate):
115+
"""
116+
Tests the change_password function on a successful API call.
117+
"""
118+
mock_authenticate.return_value = 'fake_token'
119+
mock_response = Mock()
120+
mock_response.status_code = 200
121+
mock_response.json.return_value = {'message': 'Password changed successfully'}
122+
mock_post.return_value = mock_response
123+
124+
success, answer, _, _ = change_password('old_password', 'new_password')
125+
126+
self.assertTrue(success)
127+
self.assertEqual(answer['message'], 'Password changed successfully')
128+
129+
@patch('ndi.cloud.api.implementation.auth.change_password.authenticate')
130+
@patch('requests.post')
131+
def test_change_password_failure(self, mock_post, mock_authenticate):
132+
"""
133+
Tests the change_password function on a failed API call.
134+
"""
135+
mock_authenticate.return_value = 'fake_token'
136+
mock_response = Mock()
137+
mock_response.status_code = 400
138+
mock_response.json.return_value = {'error': 'Invalid old password'}
139+
mock_post.return_value = mock_response
140+
141+
success, answer, _, _ = change_password('old_password', 'new_password')
142+
143+
self.assertFalse(success)
144+
self.assertEqual(answer['error'], 'Invalid old password')
145+
146+
@patch('requests.post')
147+
def test_resend_confirmation_success(self, mock_post):
148+
"""
149+
Tests the resend_confirmation function on a successful API call.
150+
"""
151+
mock_response = Mock()
152+
mock_response.status_code = 200
153+
mock_response.json.return_value = {'message': 'Confirmation email sent'}
154+
mock_post.return_value = mock_response
155+
156+
success, answer, _, _ = resend_confirmation('test@example.com')
157+
158+
self.assertTrue(success)
159+
self.assertEqual(answer['message'], 'Confirmation email sent')
160+
161+
@patch('requests.post')
162+
def test_resend_confirmation_failure(self, mock_post):
163+
"""
164+
Tests the resend_confirmation function on a failed API call.
165+
"""
166+
mock_response = Mock()
167+
mock_response.status_code = 400
168+
mock_response.json.return_value = {'error': 'Invalid email'}
169+
mock_post.return_value = mock_response
170+
171+
success, answer, _, _ = resend_confirmation('test@example.com')
172+
173+
self.assertFalse(success)
174+
self.assertEqual(answer['error'], 'Invalid email')
175+
176+
@patch('ndi.cloud.api.implementation.auth.reset_password.authenticate')
177+
@patch('requests.post')
178+
def test_reset_password_success(self, mock_post, mock_authenticate):
179+
"""
180+
Tests the reset_password function on a successful API call.
181+
"""
182+
mock_authenticate.return_value = 'fake_token'
183+
mock_response = Mock()
184+
mock_response.status_code = 200
185+
mock_response.json.return_value = {'message': 'Password reset email sent'}
186+
mock_post.return_value = mock_response
187+
188+
success, answer, _, _ = reset_password('test@example.com')
189+
190+
self.assertTrue(success)
191+
self.assertEqual(answer['message'], 'Password reset email sent')
192+
193+
@patch('ndi.cloud.api.implementation.auth.reset_password.authenticate')
194+
@patch('requests.post')
195+
def test_reset_password_failure(self, mock_post, mock_authenticate):
196+
"""
197+
Tests the reset_password function on a failed API call.
198+
"""
199+
mock_authenticate.return_value = 'fake_token'
200+
mock_response = Mock()
201+
mock_response.status_code = 400
202+
mock_response.json.return_value = {'error': 'Invalid email'}
203+
mock_post.return_value = mock_response
204+
205+
success, answer, _, _ = reset_password('test@example.com')
206+
207+
self.assertFalse(success)
208+
self.assertEqual(answer['error'], 'Invalid email')
209+
210+
@patch('ndi.cloud.api.implementation.auth.verify_user.authenticate')
211+
@patch('requests.post')
212+
def test_verify_user_success(self, mock_post, mock_authenticate):
213+
"""
214+
Tests the verify_user function on a successful API call.
215+
"""
216+
mock_authenticate.return_value = 'fake_token'
217+
mock_response = Mock()
218+
mock_response.status_code = 200
219+
mock_response.json.return_value = {'message': 'User verified'}
220+
mock_post.return_value = mock_response
221+
222+
success, answer, _, _ = verify_user('test@example.com', '123456')
223+
224+
self.assertTrue(success)
225+
self.assertEqual(answer['message'], 'User verified')
226+
227+
@patch('ndi.cloud.api.implementation.auth.verify_user.authenticate')
228+
@patch('requests.post')
229+
def test_verify_user_failure(self, mock_post, mock_authenticate):
230+
"""
231+
Tests the verify_user function on a failed API call.
232+
"""
233+
mock_authenticate.return_value = 'fake_token'
234+
mock_response = Mock()
235+
mock_response.status_code = 400
236+
mock_response.json.return_value = {'error': 'Invalid confirmation code'}
237+
mock_post.return_value = mock_response
238+
239+
success, answer, _, _ = verify_user('test@example.com', '123456')
240+
241+
self.assertFalse(success)
242+
self.assertEqual(answer['error'], 'Invalid confirmation code')
243+
244+
@patch('ndi.cloud.api.implementation.datasets.get_published.authenticate')
245+
@patch('requests.get')
246+
def test_get_published_success(self, mock_get, mock_authenticate):
247+
"""
248+
Tests the get_published function on a successful API call.
249+
"""
250+
mock_authenticate.return_value = 'fake_token'
251+
mock_response = Mock()
252+
mock_response.status_code = 200
253+
mock_response.json.return_value = [{'name': 'dataset1'}, {'name': 'dataset2'}]
254+
mock_get.return_value = mock_response
255+
256+
success, answer, _, _ = get_published()
257+
258+
self.assertTrue(success)
259+
self.assertEqual(len(answer), 2)
260+
261+
@patch('ndi.cloud.api.implementation.datasets.get_published.authenticate')
262+
@patch('requests.get')
263+
def test_get_published_failure(self, mock_get, mock_authenticate):
264+
"""
265+
Tests the get_published function on a failed API call.
266+
"""
267+
mock_authenticate.return_value = 'fake_token'
268+
mock_response = Mock()
269+
mock_response.status_code = 401
270+
mock_response.json.return_value = {'error': 'Unauthorized'}
271+
mock_get.return_value = mock_response
272+
273+
success, answer, _, _ = get_published()
274+
275+
self.assertFalse(success)
276+
self.assertEqual(answer['error'], 'Unauthorized')
277+
278+
@patch('ndi.cloud.api.implementation.datasets.get_unpublished.authenticate')
279+
@patch('requests.get')
280+
def test_get_unpublished_success(self, mock_get, mock_authenticate):
281+
"""
282+
Tests the get_unpublished function on a successful API call.
283+
"""
284+
mock_authenticate.return_value = 'fake_token'
285+
mock_response = Mock()
286+
mock_response.status_code = 200
287+
mock_response.json.return_value = [{'name': 'dataset3'}, {'name': 'dataset4'}]
288+
mock_get.return_value = mock_response
289+
290+
success, answer, _, _ = get_unpublished()
291+
292+
self.assertTrue(success)
293+
self.assertEqual(len(answer), 2)
294+
295+
@patch('ndi.cloud.api.implementation.datasets.get_unpublished.authenticate')
296+
@patch('requests.get')
297+
def test_get_unpublished_failure(self, mock_get, mock_authenticate):
298+
"""
299+
Tests the get_unpublished function on a failed API call.
300+
"""
301+
mock_authenticate.return_value = 'fake_token'
302+
mock_response = Mock()
303+
mock_response.status_code = 401
304+
mock_response.json.return_value = {'error': 'Unauthorized'}
305+
mock_get.return_value = mock_response
306+
307+
success, answer, _, _ = get_unpublished()
308+
309+
self.assertFalse(success)
310+
self.assertEqual(answer['error'], 'Unauthorized')
311+
312+
@patch('ndi.cloud.api.implementation.datasets.list_datasets.authenticate')
313+
@patch('requests.get')
314+
def test_list_datasets_success(self, mock_get, mock_authenticate):
315+
"""
316+
Tests the list_datasets function on a successful API call.
317+
"""
318+
mock_authenticate.return_value = 'fake_token'
319+
mock_response = Mock()
320+
mock_response.status_code = 200
321+
mock_response.json.return_value = {'datasets': [{'name': 'dataset5'}, {'name': 'dataset6'}]}
322+
mock_get.return_value = mock_response
323+
324+
success, answer, _, _ = list_datasets(cloud_organization_id='org-123')
325+
326+
self.assertTrue(success)
327+
self.assertEqual(len(answer), 2)
328+
329+
@patch('ndi.cloud.api.implementation.datasets.list_datasets.authenticate')
330+
@patch('requests.get')
331+
def test_list_datasets_failure(self, mock_get, mock_authenticate):
332+
"""
333+
Tests the list_datasets function on a failed API call.
334+
"""
335+
mock_authenticate.return_value = 'fake_token'
336+
mock_response = Mock()
337+
mock_response.status_code = 401
338+
mock_response.json.return_value = {'error': 'Unauthorized'}
339+
mock_get.return_value = mock_response
340+
341+
success, answer, _, _ = list_datasets(cloud_organization_id='org-123')
342+
343+
self.assertFalse(success)
344+
self.assertEqual(answer['error'], 'Unauthorized')
345+
346+
347+
if __name__ == '__main__':
348+
unittest.main()

0 commit comments

Comments
 (0)