1
|
|
|
"""Test kytos.core.auth module.""" |
2
|
|
|
from unittest import TestCase |
3
|
|
|
from unittest.mock import Mock |
4
|
|
|
|
5
|
|
|
import requests |
6
|
|
|
from requests.auth import HTTPBasicAuth |
7
|
|
|
|
8
|
|
|
from kytos.core.auth import Auth |
9
|
|
|
|
10
|
|
|
API_URI = "http://127.0.0.1:8181/api/kytos/core" |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
class TestAuth(TestCase): |
14
|
|
|
"""Auth tests.""" |
15
|
|
|
|
16
|
|
|
def setUp(self): |
17
|
|
|
"""Instantiate a controller and an Auth.""" |
18
|
|
|
self.controller = Mock() |
19
|
|
|
self.auth = Auth(self.controller) |
20
|
|
|
self.token = self.get_token() |
21
|
|
|
|
22
|
|
|
@classmethod |
23
|
|
|
def get_token(cls): |
24
|
|
|
"""Make a request to get a token to be used in tests""" |
25
|
|
|
success_response = requests.get( |
26
|
|
|
"%s/auth/login/" % API_URI, auth=HTTPBasicAuth("test", "test") |
27
|
|
|
) |
28
|
|
|
json_response = success_response.json() |
29
|
|
|
return json_response["token"] |
30
|
|
|
|
31
|
|
|
def validate_schema(self, my_dict, check_against): |
32
|
|
|
"""Check if a dict respects a given schema""" |
33
|
|
|
for key, value in check_against.items(): |
34
|
|
|
if isinstance(value, dict): |
35
|
|
|
return self.validate_schema(my_dict[key], value) |
36
|
|
|
if not isinstance(my_dict[key], value): |
37
|
|
|
return False |
38
|
|
|
return True |
39
|
|
|
|
40
|
|
|
def test_01_login_request(self): |
41
|
|
|
"""Test auth login endpoint""" |
42
|
|
|
success_response = requests.get( |
43
|
|
|
"%s/auth/login/" % API_URI, auth=HTTPBasicAuth("test", "test") |
44
|
|
|
) |
45
|
|
|
error_response = requests.get( |
46
|
|
|
"%s/auth/login/" % API_URI, |
47
|
|
|
auth=HTTPBasicAuth("nonexistent", "nonexistent"), |
48
|
|
|
) |
49
|
|
|
|
50
|
|
|
self.assertEqual(success_response.status_code, 200) |
51
|
|
|
self.assertEqual(error_response.status_code, 401) |
52
|
|
|
|
53
|
|
|
def test_02_list_users_request(self): |
54
|
|
|
"""Test auth list users endpoint""" |
55
|
|
|
valid_header = {"Authorization": "Bearer %s" % self.token} |
56
|
|
|
invalid_header = {"Authorization": "Bearer invalidtoken"} |
57
|
|
|
schema = {"users": list} |
58
|
|
|
success_response = requests.get( |
59
|
|
|
"%s/auth/users/" % API_URI, headers=valid_header |
60
|
|
|
) |
61
|
|
|
error_response = requests.get( |
62
|
|
|
"%s/auth/users/" % API_URI, headers=invalid_header |
63
|
|
|
) |
64
|
|
|
is_valid = self.validate_schema(success_response.json(), schema) |
65
|
|
|
|
66
|
|
|
self.assertEqual(success_response.status_code, 200) |
67
|
|
|
self.assertEqual(error_response.status_code, 401) |
68
|
|
|
self.assertTrue(is_valid) |
69
|
|
|
|
70
|
|
|
def test_03_create_user_request(self): |
71
|
|
|
"""Test auth create user endpoint""" |
72
|
|
|
header = {"Authorization": "Bearer %s" % self.token} |
73
|
|
|
data = { |
74
|
|
|
"username": "testauth_tempuser", |
75
|
|
|
"password": "testauth_tempuser", |
76
|
|
|
"email": "[email protected]", |
77
|
|
|
} |
78
|
|
|
success_response = requests.post( |
79
|
|
|
"%s/auth/users/" % API_URI, json=data, headers=header |
80
|
|
|
) |
81
|
|
|
error_response = requests.post( |
82
|
|
|
"%s/auth/users/" % API_URI, json=data, headers=header |
83
|
|
|
) |
84
|
|
|
|
85
|
|
|
self.assertEqual(success_response.status_code, 200) |
86
|
|
|
self.assertEqual(error_response.status_code, 409) |
87
|
|
|
|
88
|
|
|
def test_04_list_user_request(self): |
89
|
|
|
"""Test auth list user endpoint""" |
90
|
|
|
header = {"Authorization": "Bearer %s" % self.token} |
91
|
|
|
schema = {"data": {"email": str, "username": str}} |
92
|
|
|
success_response = requests.get( |
93
|
|
|
"%s/auth/users/%s" % (API_URI, "testauth_tempuser"), headers=header |
94
|
|
|
) |
95
|
|
|
error_response = requests.get( |
96
|
|
|
"%s/auth/users/%s" % (API_URI, "nonexistent"), headers=header |
97
|
|
|
) |
98
|
|
|
is_valid = self.validate_schema(success_response.json(), schema) |
99
|
|
|
|
100
|
|
|
self.assertEqual(success_response.status_code, 200) |
101
|
|
|
self.assertEqual(error_response.status_code, 500) |
102
|
|
|
self.assertTrue(is_valid) |
103
|
|
|
|
104
|
|
|
def test_05_update_user_request(self): |
105
|
|
|
"""Test auth update user endpoint""" |
106
|
|
|
header = {"Authorization": "Bearer %s" % self.token} |
107
|
|
|
data = {"email": "[email protected]"} |
108
|
|
|
success_response = requests.patch( |
109
|
|
|
"%s/auth/users/%s" % (API_URI, "testauth_tempuser"), |
110
|
|
|
json=data, |
111
|
|
|
headers=header, |
112
|
|
|
) |
113
|
|
|
error_response = requests.patch( |
114
|
|
|
"%s/auth/users/%s" % (API_URI, "nonexistent"), |
115
|
|
|
json=data, |
116
|
|
|
headers=header, |
117
|
|
|
) |
118
|
|
|
|
119
|
|
|
self.assertEqual(success_response.status_code, 200) |
120
|
|
|
self.assertEqual(error_response.status_code, 500) |
121
|
|
|
|
122
|
|
|
def test_06_delete_user_request(self): |
123
|
|
|
"""Test auth delete user endpoint""" |
124
|
|
|
header = {"Authorization": "Bearer %s" % self.token} |
125
|
|
|
success_response = requests.delete( |
126
|
|
|
"%s/auth/users/%s" % (API_URI, "testauth_tempuser"), headers=header |
127
|
|
|
) |
128
|
|
|
error_response = requests.delete( |
129
|
|
|
"%s/auth/users/%s" % (API_URI, "nonexistent"), headers=header |
130
|
|
|
) |
131
|
|
|
|
132
|
|
|
self.assertEqual(success_response.status_code, 200) |
133
|
|
|
self.assertEqual(error_response.status_code, 500) |
134
|
|
|
|