This repository has been archived by the owner on Oct 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_api.py
53 lines (42 loc) · 1.95 KB
/
test_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import unittest
from unittest.mock import patch, MagicMock
from app import get_user_by_otp, delete_user_by_otp
class TestAPICalls(unittest.TestCase):
@patch('app.requests.get')
def test_get_user_by_otp_success(self, mock_get):
mock_response = MagicMock()
mock_response.status_code = 200
mock_response.json.return_value = {'id': 1, 'name': 'John Doe', 'otp': '123456', 'vector': 'base64vector'}
mock_get.return_value = mock_response
user = get_user_by_otp('123456')
self.assertEqual(user, {'id': 1, 'name': 'John Doe', 'otp': '123456', 'vector': 'base64vector'})
@patch('app.requests.get')
def test_get_user_by_otp_not_found(self, mock_get):
mock_response = MagicMock()
mock_response.status_code = 404
mock_get.return_value = mock_response
user = get_user_by_otp('invalid_otp')
self.assertIsNone(user)
@patch('app.requests.get')
@patch('app.requests.delete')
def test_delete_user_by_otp_success(self, mock_delete, mock_get):
mock_get_response = MagicMock()
mock_get_response.status_code = 200
mock_get_response.json.return_value = {'id': 1, 'name': 'John Doe', 'otp': '123456', 'vector': 'base64vector'}
mock_get.return_value = mock_get_response
mock_delete_response = MagicMock()
mock_delete_response.status_code = 200
mock_delete.return_value = mock_delete_response
delete_user_by_otp('123456')
mock_delete.assert_called_once()
# Test for delete_user_by_otp when user is not found
@patch('app.requests.get')
@patch('app.requests.delete')
def test_delete_user_by_otp_not_found(self, mock_delete, mock_get):
mock_get_response = MagicMock()
mock_get_response.status_code = 404
mock_get.return_value = mock_get_response
delete_user_by_otp('invalid_otp')
mock_delete.assert_not_called()
if __name__ == '__main__':
unittest.main()