-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathtest_auth.py
More file actions
299 lines (252 loc) · 10 KB
/
test_auth.py
File metadata and controls
299 lines (252 loc) · 10 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import json
import os
import responses
from beautifultable import BeautifulTable
from click.testing import CliRunner
from termcolor import colored
from evalai.login import login
from evalai.challenges import challenge, challenges
from evalai.set_host import host
from evalai.utils.urls import URLS
from evalai.utils.config import (
API_HOST_URL,
AUTH_TOKEN_DIR,
AUTH_TOKEN_FILE_NAME,
AUTH_TOKEN_PATH,
HOST_URL_FILE_PATH,
)
from evalai.utils.common import convert_UTC_date_to_local
from tests.data import challenge_response, auth_response
from tests.base import BaseTestClass
class TestGetUserAuthToken(BaseTestClass):
token_file = os.path.join(AUTH_TOKEN_DIR, AUTH_TOKEN_FILE_NAME)
def setup(self):
with open(self.token_file) as fo:
self.token = fo.read()
os.remove(self.token_file)
def teardown(self):
with open(self.token_file, "w") as f:
f.write(self.token)
def test_get_user_auth_token_when_file_does_not_exist(self):
expected = (
"\nThe authentication token json file doesn't exists at the required path. "
"Please download the file from the Profile section of the EvalAI webapp and "
"place it at ~/.evalai/token.json\n\n"
)
runner = CliRunner()
result = runner.invoke(challenges)
response = result.output
assert response == expected
class TestUserRequestWithInvalidToken(BaseTestClass):
def setup(self):
invalid_token_data = json.loads(challenge_response.invalid_token)
url = "{}{}"
responses.add(
responses.GET,
url.format(API_HOST_URL, URLS.challenge_list.value),
json=invalid_token_data,
status=401,
)
responses.add(
responses.GET,
url.format(API_HOST_URL, URLS.host_teams.value),
json=invalid_token_data,
status=401,
)
responses.add(
responses.GET,
url.format(API_HOST_URL, URLS.leaderboard.value).format("1"),
json=invalid_token_data,
status=401,
)
self.expected = "\nThe authentication token you are using isn't valid. Please generate it again.\n\n"
@responses.activate
def test_display_all_challenge_lists_when_token_is_invalid(self):
runner = CliRunner()
result = runner.invoke(challenges)
response = result.output
assert response == self.expected
@responses.activate
def test_display_leaderboard_when_token_is_invalid(self):
runner = CliRunner()
result = runner.invoke(challenge, ["2", "leaderboard", "1"])
response = result.output
assert response == self.expected
@responses.activate
def test_display_participant_challenge_lists_when_token_is_invalid(self):
expected = "The authentication token you are using isn't valid. Please generate it again."
runner = CliRunner()
result = runner.invoke(challenges, ["--host"])
response = result.output.strip()
assert response == expected
class TestUserRequestWithExpiredToken(BaseTestClass):
def setup(self):
token_expired_data = json.loads(challenge_response.token_expired)
url = "{}{}"
responses.add(
responses.GET,
url.format(API_HOST_URL, URLS.challenge_list.value),
json=token_expired_data,
status=401,
)
@responses.activate
def test_display_all_challenge_lists_when_token_has_expired(self):
expected = (
"\nSorry, the token has expired. Please generate it again.\n\n"
)
runner = CliRunner()
result = runner.invoke(challenges)
response = result.output
assert response == expected
class TestHostConfig(BaseTestClass):
def teardown(self):
if os.path.exists(HOST_URL_FILE_PATH):
os.remove(HOST_URL_FILE_PATH)
def test_get_default_host(self):
expected = (
"You haven't configured a Host URL for the CLI.\n"
"The CLI would be using https://eval.ai as the default url.\n"
)
runner = CliRunner()
result = runner.invoke(host)
assert expected == result.output
assert result.exit_code == 0
def test_set_host_wrong_url(self):
expected = (
"Sorry, please enter a valid url.\n" "Example: https://eval.ai\n"
)
runner = CliRunner()
result = runner.invoke(host, ["-sh", "https:/evalai.cloudcv"])
assert expected == result.output
assert result.exit_code == 0
def test_set_host_url(self):
expected = "{} is set as the host url.\n".format("https://eval.ai")
runner = CliRunner()
result = runner.invoke(host, ["-sh", "https://eval.ai"])
assert expected == result.output
assert result.exit_code == 0
def test_set_host_url_and_display(self):
expected = "https://eval.ai is the Host URL of EvalAI.\n"
runner = CliRunner()
runner.invoke(host, ["-sh", "https://eval.ai"])
result = runner.invoke(host)
assert expected == result.output
assert result.exit_code == 0
class TestSetAndLoadHostURL(BaseTestClass):
def setup(self):
challenge_data = json.loads(challenge_response.challenges)
url = "{}{}"
responses.add(
responses.GET,
url.format("https://eval.ai", URLS.challenge_list.value),
json=challenge_data,
status=200,
)
self.output = ""
challenge_data = challenge_data["results"]
table = BeautifulTable(max_width=200)
attributes = ["id", "title", "short_description"]
columns_attributes = [
"ID",
"Title",
"Short Description",
"Creator",
"Start Date",
"End Date",
]
table.column_headers = columns_attributes
for challenge_json in reversed(challenge_data):
values = list(map(lambda item: challenge_json[item], attributes))
creator = challenge_json["creator"]["team_name"]
start_date = convert_UTC_date_to_local(
challenge_json["start_date"]
)
end_date = convert_UTC_date_to_local(challenge_json["end_date"])
values.extend([creator, start_date, end_date])
table.append_row(
[
colored(values[0], "white"),
colored(values[1], "yellow"),
colored(values[2], "cyan"),
colored(values[3], "white"),
colored(values[4], "green"),
colored(values[5], "red"),
]
)
self.output = str(table)
def teardown(self):
if os.path.exists(HOST_URL_FILE_PATH):
os.remove(HOST_URL_FILE_PATH)
@responses.activate
def test_set_and_load_host_url(self):
runner = CliRunner()
result = runner.invoke(host, ["-sh", "https://eval.ai"])
result = runner.invoke(challenges)
response = result.output.strip()
assert str(response) == self.output
class TestLogin(BaseTestClass):
def setup(self):
url = "{}{}"
valid_login_response = json.loads(auth_response.valid_login_response)
valid_login_body = json.loads(auth_response.valid_login_body)
responses.add(
responses.POST,
url.format("https://eval.ai", URLS.login.value),
json=valid_login_response,
match=[
responses.urlencoded_params_matcher(valid_login_body)
],
status=200
)
invalid_login_response = json.loads(auth_response.invalid_login_response)
invalid_login_body = json.loads(auth_response.invalid_login_body)
responses.add(
responses.POST,
url.format("https://eval.ai", URLS.login.value),
json=invalid_login_response,
match=[
responses.urlencoded_params_matcher(invalid_login_body)
],
status=400
)
get_access_token_response = json.loads(auth_response.get_access_token_response)
get_access_token_headers = json.loads(auth_response.get_access_token_headers)
responses.add(
responses.GET,
url.format("https://eval.ai", URLS.get_access_token.value),
json=get_access_token_response,
headers=get_access_token_headers,
status=200
)
def teardown(self):
if os.path.exists(HOST_URL_FILE_PATH):
os.remove(HOST_URL_FILE_PATH)
@responses.activate
def test_login(self):
json.loads(auth_response.get_access_token_response)
expected = "\nLogged in successfully!"
runner = CliRunner()
result = runner.invoke(host, ["-sh", "https://eval.ai"])
result = runner.invoke(login, input="host\npassword")
response = result.output.strip()
assert expected in str(response)
assert os.path.exists(AUTH_TOKEN_PATH), "Auth Token is not set"
# Checking if the token is equal to what was set during login
with open(str(AUTH_TOKEN_PATH), "r") as TokenFile:
assert json.loads(TokenFile.read()) == json.loads(auth_response.get_access_token_response)
@responses.activate
def test_login_when_httperror(self):
expected = "\nCould not establish a connection to EvalAI. Please check the Host URL."
runner = CliRunner()
result = runner.invoke(host, ["-sh", "https://evalaiwrongurl.ai"])
result = runner.invoke(login, input="host\npassword")
response = result.output.strip()
assert expected in str(response)
@responses.activate
def test_login_when_wrong_credentials(self):
expected = "\nUnable to log in with provided credentials."
runner = CliRunner()
result = runner.invoke(host, ["-sh", "https://eval.ai"])
result = runner.invoke(login, input="notahost\nnotapassword")
response = result.output.strip()
assert expected in str(response)