This repository has been archived by the owner on Nov 4, 2020. It is now read-only.
/
test_common.py
189 lines (153 loc) · 6.02 KB
/
test_common.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
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
from mock import Mock
import requests
import responses
from requests.exceptions import HTTPError
from gitlab import Gitlab
from gitlab_tests.base import BaseTest
from response_data.common import *
from response_data.users import *
class TestSuccessOrRaise(BaseTest):
def test_success_or_raise_without_error(self):
response = Mock()
response_config = {
'status_code': 200,
'json.return_value': post_users_error
}
response.configure_mock(**response_config)
self.gitlab.success_or_raise(response, [200])
def test_success_or_raise_with_error(self):
response = requests.models.Response()
response.status_code = 400
response._content = post_users_error
self.gitlab.suppress_http_error = False
self.assertRaises(HTTPError, self.gitlab.success_or_raise, response)
self.gitlab.suppress_http_error = True
class TestLogin(BaseTest):
@responses.activate
def test_login(self):
responses.add(
responses.POST,
self.gitlab.api_url + '/session',
json=login,
status=201,
content_type='application/json')
self.assertEqual(
login, self.gitlab.login(user=self.user, password=self.password))
@responses.activate
def test_login_email(self):
responses.add(
responses.POST,
self.gitlab.api_url + '/session',
json=login,
status=201,
content_type='application/json')
self.assertEqual(
login, self.gitlab.login(email='admin@example.com', password='test'))
@responses.activate
def test_login_with_no_values(self):
responses.add(
responses.POST,
self.gitlab.api_url + '/session',
body=get_users,
status=404,
content_type='application/json')
self.assertRaises(ValueError, self.gitlab.login)
class TestGet(BaseTest):
@responses.activate
def test_get_with_200(self):
responses.add(
responses.GET,
self.gitlab.api_url + '/users',
json=get_users,
status=200,
content_type='application/json')
self.assertEqual(get_users, self.gitlab.get('/users'))
self.assertEqual(get_users, self.gitlab.get('/users', default_response={}))
@responses.activate
def test_get_with_404(self):
responses.add(
responses.GET,
self.gitlab.api_url + '/users',
body='{"error": "Not here"}',
status=404,
content_type='application/json')
self.gitlab.suppress_http_error = False
self.assertRaises(HTTPError, self.gitlab.get, '/users')
self.gitlab.suppress_http_error = True
class TestPost(BaseTest):
@responses.activate
def test_post_with_201(self):
responses.add(
responses.POST,
self.gitlab.api_url + '/users',
json=post_users,
status=201,
content_type='application/json')
data = {
'name': 'test',
'username': 'test1',
'password': 'MyTestPassword1',
'email': 'example@example.com'
}
self.assertEqual(post_users, self.gitlab.post('/users', **data))
self.assertEqual(post_users, self.gitlab.post('/users', default_response={}, **data))
@responses.activate
def test_get_with_404(self):
responses.add(
responses.POST,
self.gitlab.api_url + '/users',
body=post_users_error,
status=409,
content_type='application/json')
data = {
'name': 'test',
'username': 'test1',
'password': 'MyTestPassword1',
'email': 'example@example.com'
}
self.gitlab.suppress_http_error = False
self.assertRaises(HTTPError, self.gitlab.post, '/users', **data)
self.gitlab.suppress_http_error = True
class TestDelete(BaseTest):
@responses.activate
def test_delete(self):
responses.add(
responses.DELETE,
self.gitlab.api_url + '/users/5',
json=None,
status=204,
content_type='application/json')
self.assertEqual({}, self.gitlab.delete('/users/5'))
self.assertEqual({}, self.gitlab.delete('/users/5', default_response={}))
@responses.activate
def test_delete_404(self):
responses.add(
responses.POST,
self.gitlab.api_url + '/users',
body=post_users_error,
status=409,
content_type='application/json')
self.gitlab.suppress_http_error = False
self.assertRaises(HTTPError, self.gitlab.post, '/users')
self.gitlab.suppress_http_error = True
class TestFormatString(BaseTest):
def test__format_string(self):
self.assertEqual('foo%2Fbar', self.gitlab._format_string('foo/bar'))
self.assertEqual(1, self.gitlab._format_string(1))
class TestInit(BaseTest):
def test___init__with_token(self):
gitlab = Gitlab('http://localhost:10080', verify_ssl=False, token='something')
self.assertEqual('something', gitlab.token)
self.assertEqual({'PRIVATE-TOKEN': 'something'}, gitlab.headers)
def test___init__with_oauth_token(self):
gitlab = Gitlab('http://localhost:10080', verify_ssl=False, oauth_token='something')
self.assertEqual('something', gitlab.oauth_token)
self.assertEqual({'Authorization': 'Bearer something'}, gitlab.headers)
def test___init__without_host(self):
self.assertRaises(ValueError, Gitlab, None, verify_ssl=False, oauth_token='something')
def test___init__without_protocol(self):
gitlab = Gitlab('localhost:10080', verify_ssl=False, oauth_token='something')
self.assertEqual('https://localhost:10080', gitlab.host)
def test___init__with_https(self):
gitlab = Gitlab('https://localhost:10080', verify_ssl=False, oauth_token='something')
self.assertEqual('https://localhost:10080', gitlab.host)