-
-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathtest_public_keys.py
More file actions
288 lines (266 loc) · 11.3 KB
/
test_public_keys.py
File metadata and controls
288 lines (266 loc) · 11.3 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
import uuid
from datetime import datetime, timezone
from unittest.mock import AsyncMock
import pytest
from freezegun import freeze_time
from httpx import AsyncClient
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from dstack._internal.server.models import UserPublicKeyModel
from dstack._internal.server.testing.common import (
create_user,
create_user_public_key,
get_auth_headers,
)
from dstack._internal.server.testing.matchers import SomeUUID4Str
@pytest.mark.asyncio
class TestListUserPublicKeys:
async def test_returns_40x_if_not_authenticated(self, client: AsyncClient):
response = await client.post("/api/users/public_keys/list")
assert response.status_code in [401, 403]
@pytest.mark.parametrize("test_db", ["sqlite", "postgres"], indirect=True)
@pytest.mark.usefixtures("test_db")
async def test_lists_own_public_keys(self, session: AsyncSession, client: AsyncClient):
user = await create_user(session=session)
key = await create_user_public_key(
session=session,
user=user,
name="my-key",
type="ssh-ed25519",
fingerprint="SHA256:testfingerprint",
created_at=datetime(2023, 1, 2, 3, 4, tzinfo=timezone.utc),
)
response = await client.post(
"/api/users/public_keys/list",
headers=get_auth_headers(user.token),
)
assert response.status_code == 200
assert response.json() == [
{
"id": str(key.id),
"added_at": "2023-01-02T03:04:00+00:00",
"name": "my-key",
"type": "ssh-ed25519",
"fingerprint": "SHA256:testfingerprint",
}
]
@pytest.mark.parametrize("test_db", ["sqlite", "postgres"], indirect=True)
@pytest.mark.usefixtures("test_db")
async def test_does_not_list_other_users_keys(
self, session: AsyncSession, client: AsyncClient
):
user = await create_user(session=session)
other_user = await create_user(session=session, name="other_user")
await create_user_public_key(session=session, user=other_user)
response = await client.post(
"/api/users/public_keys/list",
headers=get_auth_headers(user.token),
)
assert response.status_code == 200
assert response.json() == []
@pytest.mark.parametrize("test_db", ["sqlite", "postgres"], indirect=True)
@pytest.mark.usefixtures("test_db")
async def test_returns_keys_in_reverse_chronological_order(
self, session: AsyncSession, client: AsyncClient
):
user = await create_user(session=session)
key1 = await create_user_public_key(
session=session,
user=user,
name="older-key",
fingerprint="SHA256:fingerprint1",
created_at=datetime(2023, 1, 2, 3, 4, tzinfo=timezone.utc),
)
key2 = await create_user_public_key(
session=session,
user=user,
name="newer-key",
fingerprint="SHA256:fingerprint2",
created_at=datetime(2023, 1, 2, 3, 5, tzinfo=timezone.utc),
)
response = await client.post(
"/api/users/public_keys/list",
headers=get_auth_headers(user.token),
)
assert response.status_code == 200
data = response.json()
assert len(data) == 2
assert data[0]["id"] == str(key2.id)
assert data[1]["id"] == str(key1.id)
@pytest.mark.asyncio
class TestAddUserPublicKey:
PUBLIC_KEY_NO_COMMENT = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAA"
PUBLIC_KEY = f"{PUBLIC_KEY_NO_COMMENT} test@example.com"
@pytest.fixture
def validate_openssh_public_key_mock(self, monkeypatch: pytest.MonkeyPatch) -> AsyncMock:
mock = AsyncMock()
monkeypatch.setattr(
"dstack._internal.server.services.public_keys.validate_openssh_public_key", mock
)
return mock
async def test_returns_40x_if_not_authenticated(self, client: AsyncClient):
response = await client.post(
"/api/users/public_keys/add",
json={"key": self.PUBLIC_KEY},
)
assert response.status_code in [401, 403]
@pytest.mark.parametrize("test_db", ["sqlite", "postgres"], indirect=True)
@pytest.mark.usefixtures("test_db")
@freeze_time(datetime(2023, 1, 2, 3, 4, tzinfo=timezone.utc))
async def test_adds_valid_public_key(
self,
session: AsyncSession,
client: AsyncClient,
validate_openssh_public_key_mock: AsyncMock,
):
user = await create_user(session=session)
response = await client.post(
"/api/users/public_keys/add",
headers=get_auth_headers(user.token),
json={"key": self.PUBLIC_KEY},
)
assert response.status_code == 200
assert response.json() == {
"id": SomeUUID4Str(),
"type": "ssh-ed25519",
"name": "test@example.com",
"fingerprint": "SHA256:uALbfMqe7g4MMaRS5NMJen38dAEHwtxzR0iX0Ymuc80",
"added_at": "2023-01-02T03:04:00+00:00",
}
validate_openssh_public_key_mock.assert_awaited_once_with(self.PUBLIC_KEY)
@pytest.mark.parametrize("test_db", ["sqlite", "postgres"], indirect=True)
@pytest.mark.usefixtures("test_db")
@pytest.mark.usefixtures("validate_openssh_public_key_mock")
async def test_adds_key_with_custom_name(self, session: AsyncSession, client: AsyncClient):
user = await create_user(session=session)
response = await client.post(
"/api/users/public_keys/add",
headers=get_auth_headers(user.token),
json={"key": self.PUBLIC_KEY, "name": "my-laptop"},
)
assert response.status_code == 200
assert response.json()["name"] == "my-laptop"
@pytest.mark.parametrize("test_db", ["sqlite", "postgres"], indirect=True)
@pytest.mark.usefixtures("test_db")
@pytest.mark.usefixtures("validate_openssh_public_key_mock")
async def test_uses_md5_as_name_when_no_comment_and_no_name(
self, session: AsyncSession, client: AsyncClient
):
user = await create_user(session=session)
response = await client.post(
"/api/users/public_keys/add",
headers=get_auth_headers(user.token),
json={"key": self.PUBLIC_KEY_NO_COMMENT},
)
assert response.status_code == 200
assert response.json()["name"] == "744e414c6ac55e3f15c1dd48229cbe74"
@pytest.mark.parametrize("test_db", ["sqlite", "postgres"], indirect=True)
@pytest.mark.usefixtures("test_db")
@pytest.mark.parametrize(
"key",
[
pytest.param("sha-rsa-invalid", id="only-one-field"),
pytest.param("ssh-rsa AAAAB3NzaC1kc3M=", id="dsa-declared-as-rsa"),
],
)
async def test_returns_400_for_invalid_key(
self, session: AsyncSession, client: AsyncClient, key: str
):
user = await create_user(session=session)
response = await client.post(
"/api/users/public_keys/add",
headers=get_auth_headers(user.token),
json={"key": key},
)
assert response.status_code == 400
assert "Invalid public key" in response.json()["detail"][0]["msg"]
@pytest.mark.parametrize("test_db", ["sqlite", "postgres"], indirect=True)
@pytest.mark.usefixtures("test_db")
async def test_returns_400_for_unsupported_key(
self, session: AsyncSession, client: AsyncClient
):
user = await create_user(session=session)
response = await client.post(
"/api/users/public_keys/add",
headers=get_auth_headers(user.token),
json={"key": "ssh-dss AAAAB3NzaC1kc3M="},
)
assert response.status_code == 400
assert response.json()["detail"][0]["msg"] == "Unsupported key type: ssh-dss"
@pytest.mark.parametrize("test_db", ["sqlite", "postgres"], indirect=True)
@pytest.mark.usefixtures("test_db")
@pytest.mark.usefixtures("validate_openssh_public_key_mock")
async def test_returns_400_resource_exists_for_duplicate_key(
self, session: AsyncSession, client: AsyncClient
):
user = await create_user(session=session)
response = await client.post(
"/api/users/public_keys/add",
headers=get_auth_headers(user.token),
json={"key": self.PUBLIC_KEY},
)
assert response.status_code == 200
response = await client.post(
"/api/users/public_keys/add",
headers=get_auth_headers(user.token),
# The same key, the comment does not matter
json={"key": self.PUBLIC_KEY_NO_COMMENT},
)
assert response.status_code == 400
assert response.json()["detail"][0]["code"] == "resource_exists"
@pytest.mark.asyncio
class TestDeleteUserPublicKeys:
async def test_returns_40x_if_not_authenticated(self, client: AsyncClient):
response = await client.post(
"/api/users/public_keys/delete",
json={"ids": [str(uuid.uuid4())]},
)
assert response.status_code in [401, 403]
@pytest.mark.parametrize("test_db", ["sqlite", "postgres"], indirect=True)
@pytest.mark.usefixtures("test_db")
async def test_deletes_public_key(self, session: AsyncSession, client: AsyncClient):
user = await create_user(session=session)
key = await create_user_public_key(session=session, user=user)
other_key = await create_user_public_key(
session=session, user=user, fingerprint="SHA256:other"
)
response = await client.post(
"/api/users/public_keys/delete",
headers=get_auth_headers(user.token),
json={"ids": [str(key.id)]},
)
assert response.status_code == 200
res = await session.execute(
select(UserPublicKeyModel).where(UserPublicKeyModel.user_id == user.id)
)
assert res.scalars().all() == [other_key]
@pytest.mark.parametrize("test_db", ["sqlite", "postgres"], indirect=True)
@pytest.mark.usefixtures("test_db")
async def test_silently_ignores_nonexistent_ids(
self, session: AsyncSession, client: AsyncClient
):
user = await create_user(session=session)
response = await client.post(
"/api/users/public_keys/delete",
headers=get_auth_headers(user.token),
json={"ids": [str(uuid.uuid4())]},
)
assert response.status_code == 200
@pytest.mark.parametrize("test_db", ["sqlite", "postgres"], indirect=True)
@pytest.mark.usefixtures("test_db")
async def test_does_not_delete_other_users_keys(
self, session: AsyncSession, client: AsyncClient
):
user = await create_user(session=session)
other_user = await create_user(session=session, name="other_user")
other_user_key = await create_user_public_key(session=session, user=other_user)
response = await client.post(
"/api/users/public_keys/delete",
headers=get_auth_headers(user.token),
json={"ids": [str(other_user_key.id)]},
)
assert response.status_code == 200
res = await session.execute(
select(UserPublicKeyModel).where(UserPublicKeyModel.user_id == other_user.id)
)
assert res.scalars().all() == [other_user_key]