-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathtest_ssh_keys.py
More file actions
167 lines (132 loc) · 5.28 KB
/
test_ssh_keys.py
File metadata and controls
167 lines (132 loc) · 5.28 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
import pytest
import responses # https://github.com/getsentry/responses
from verda.exceptions import APIException
from verda.ssh_keys import SSHKey, SSHKeysService
INVALID_REQUEST = 'invalid_request'
INVALID_REQUEST_MESSAGE = 'Your existence is invalid'
KEY_ID = '01cf5dc1-a5d2-4972-ae4e-d429115d055b'
KEY_NAME = 'key to your heart'
KEY_VALUE = 'asdf'
KEY_ID_2 = '12345dc1-a5d2-4972-ae4e-d429115d055b'
PAYLOAD = [{'id': KEY_ID, 'name': KEY_NAME, 'key': KEY_VALUE}]
class TestSSHKeys:
@pytest.fixture
def ssh_key_service(self, http_client):
return SSHKeysService(http_client)
@pytest.fixture
def endpoint(self, http_client):
return http_client._base_url + '/sshkeys'
def test_get_keys(self, ssh_key_service, endpoint):
# arrange - add response mock
responses.add(responses.GET, endpoint, json=PAYLOAD, status=200)
# act
keys = ssh_key_service.get()
# assert
assert isinstance(keys, list)
assert len(keys) == 1
assert isinstance(keys[0], SSHKey)
assert keys[0].id == KEY_ID
assert keys[0].name == KEY_NAME
assert keys[0].public_key == KEY_VALUE
assert responses.assert_call_count(endpoint, 1) is True
def test_get_key_by_id_successful(self, ssh_key_service, endpoint):
# arrange - add response mock
url = endpoint + '/' + KEY_ID
responses.add(responses.GET, url, json=PAYLOAD, status=200)
# act
key = ssh_key_service.get_by_id(KEY_ID)
# assert
assert isinstance(key, SSHKey)
assert key.id == KEY_ID
assert key.name == KEY_NAME
assert key.public_key == KEY_VALUE
assert responses.assert_call_count(url, 1) is True
def test_get_key_by_id_failed(self, ssh_key_service, endpoint):
# arrange - add response mock
url = endpoint + '/x'
responses.add(
responses.GET,
url,
json={'code': INVALID_REQUEST, 'message': INVALID_REQUEST_MESSAGE},
status=400,
)
# act
with pytest.raises(APIException) as excinfo:
ssh_key_service.get_by_id('x')
# assert
assert excinfo.value.code == INVALID_REQUEST
assert excinfo.value.message == INVALID_REQUEST_MESSAGE
assert responses.assert_call_count(url, 1) is True
def test_create_key_successful(self, ssh_key_service, endpoint):
# arrange - add response mock
responses.add(responses.POST, endpoint, body=KEY_ID, status=201)
# act
key = ssh_key_service.create(KEY_NAME, KEY_VALUE)
# assert
assert isinstance(key, SSHKey)
assert isinstance(key.id, str)
assert key.id == KEY_ID
assert responses.assert_call_count(endpoint, 1) is True
def test_create_key_failed(self, ssh_key_service, endpoint):
# arrange - add response mock
responses.add(
responses.POST,
endpoint,
json={'code': INVALID_REQUEST, 'message': INVALID_REQUEST_MESSAGE},
status=400,
)
# act
with pytest.raises(APIException) as excinfo:
ssh_key_service.create(KEY_NAME, KEY_VALUE)
# assert
assert excinfo.value.code == INVALID_REQUEST
assert excinfo.value.message == INVALID_REQUEST_MESSAGE
assert responses.assert_call_count(endpoint, 1) is True
def test_delete_keys_successful(self, ssh_key_service, endpoint):
# arrange - add response mock
responses.add(responses.DELETE, endpoint, status=200)
# act
result = ssh_key_service.delete([KEY_ID, KEY_ID_2])
# assert
assert result is None
assert responses.assert_call_count(endpoint, 1) is True
def test_delete_keys_failed(self, ssh_key_service, endpoint):
# arrange - add response mock
responses.add(
responses.DELETE,
endpoint,
json={'code': INVALID_REQUEST, 'message': INVALID_REQUEST_MESSAGE},
status=400,
)
# act
with pytest.raises(APIException) as excinfo:
ssh_key_service.delete(['x'])
# assert
assert excinfo.value.code == INVALID_REQUEST
assert excinfo.value.message == INVALID_REQUEST_MESSAGE
assert responses.assert_call_count(endpoint, 1) is True
def test_delete_key_by_id_successful(self, ssh_key_service, endpoint):
# arrange - add response mock
url = endpoint + '/' + KEY_ID
responses.add(responses.DELETE, url, status=200)
# act
result = ssh_key_service.delete_by_id(KEY_ID)
# assert
assert result is None
assert responses.assert_call_count(url, 1) is True
def test_delete_key_by_id_failed(self, ssh_key_service, endpoint):
# arrange - add response mock
url = endpoint + '/x'
responses.add(
responses.DELETE,
url,
json={'code': INVALID_REQUEST, 'message': INVALID_REQUEST_MESSAGE},
status=400,
)
# act
with pytest.raises(APIException) as excinfo:
ssh_key_service.delete_by_id('x')
# assert
assert excinfo.value.code == INVALID_REQUEST
assert excinfo.value.message == INVALID_REQUEST_MESSAGE
assert responses.assert_call_count(url, 1) is True