-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_push.py
More file actions
109 lines (97 loc) · 4.39 KB
/
test_push.py
File metadata and controls
109 lines (97 loc) · 4.39 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
"""
Unit tests for the Deis api app.
Run the tests with "./manage.py test api"
"""
from __future__ import unicode_literals
import json
from django.test import TestCase
from django.test.utils import override_settings
from deis import settings
@override_settings(CELERY_ALWAYS_EAGER=True)
class PushTest(TestCase):
"""Tests pushes into the push system"""
fixtures = ['tests.json']
def setUp(self):
self.assertTrue(
self.client.login(username='autotest', password='password'))
url = '/api/providers'
creds = {'secret_key': 'x' * 64, 'access_key': 1 * 20}
body = {'id': 'autotest', 'type': 'mock', 'creds': json.dumps(creds)}
response = self.client.post(url, json.dumps(body), content_type='application/json')
self.assertEqual(response.status_code, 201)
url = '/api/flavors'
body = {
'id': 'autotest',
'provider': 'autotest',
'params': json.dumps({'region': 'us-west-2'}),
}
response = self.client.post(url, json.dumps(body), content_type='application/json')
self.assertEqual(response.status_code, 201)
response = self.client.post('/api/formations', json.dumps(
{'id': 'autotest', 'domain': 'localhost.localdomain'}),
content_type='application/json')
self.assertEqual(response.status_code, 201)
def test_push(self):
"""
Test that a user can push into the system
"""
url = '/api/apps'
body = {'formation': 'autotest'}
response = self.client.post(url, json.dumps(body), content_type='application/json')
self.assertEqual(response.status_code, 201)
app_id = response.data['id']
# prepare a push body
body = {
'sha': 'df1e628f2244b73f9cdf944f880a2b3470a122f4',
'fingerprint': '88:25:ed:67:56:91:3d:c6:1b:7f:42:c6:9b:41:24:80',
'receive_user': 'autotest',
'receive_repo': 'repo.git',
'ssh_connection': '10.0.1.10 50337 172.17.0.143 22',
'ssh_original_command': "git-receive-pack 'repo.git'",
}
# post a request without the auth header
url = "/api/apps/{app_id}/push".format(**locals())
response = self.client.post(url, json.dumps(body), content_type='application/json')
self.assertEqual(response.status_code, 403)
# now try with the builder key in the special auth header
response = self.client.post(url, json.dumps(body), content_type='application/json',
HTTP_X_DEIS_BUILDER_AUTH=settings.BUILDER_KEY)
self.assertEqual(response.status_code, 201)
for k in ('owner', 'app', 'sha', 'fingerprint', 'receive_repo', 'receive_user',
'ssh_connection', 'ssh_original_command'):
self.assertIn(k, response.data)
def test_push_abuse(self):
# create a legit app
url = '/api/apps'
body = {'formation': 'autotest'}
response = self.client.post(url, json.dumps(body), content_type='application/json')
self.assertEqual(response.status_code, 201)
app_id = response.data['id']
# register an evil user
username, password = 'eviluser', 'password'
first_name, last_name = 'Evil', 'User'
email = 'evil@deis.io'
submit = {
'username': username,
'password': password,
'first_name': first_name,
'last_name': last_name,
'email': email,
}
url = '/api/auth/register'
response = self.client.post(url, json.dumps(submit), content_type='application/json')
self.assertEqual(response.status_code, 201)
# prepare a push body that simulates a git push
body = {
'sha': 'df1e628f2244b73f9cdf944f880a2b3470a122f4',
'fingerprint': '88:25:ed:67:56:91:3d:c6:1b:7f:42:c6:9b:41:24:99',
'receive_user': 'eviluser',
'receive_repo': 'repo.git',
'ssh_connection': '10.0.1.10 50337 172.17.0.143 22',
'ssh_original_command': "git-receive-pack 'repo.git'",
}
# try to push as "eviluser"
url = "/api/apps/{app_id}/push".format(**locals())
response = self.client.post(url, json.dumps(body), content_type='application/json',
HTTP_X_DEIS_BUILDER_AUTH=settings.BUILDER_KEY)
self.assertEqual(response.status_code, 403)