|
| 1 | +""" |
| 2 | +Unit tests for the Deis api app. |
| 3 | +
|
| 4 | +Run the tests with "./manage.py test api" |
| 5 | +""" |
| 6 | + |
| 7 | +from __future__ import unicode_literals |
| 8 | + |
| 9 | +import json |
| 10 | + |
| 11 | +from django.test import TestCase |
| 12 | +from django.test.utils import override_settings |
| 13 | + |
| 14 | +from deis import settings |
| 15 | + |
| 16 | + |
| 17 | +@override_settings(CELERY_ALWAYS_EAGER=True) |
| 18 | +class PushTest(TestCase): |
| 19 | + |
| 20 | + """Tests pushes into the push system""" |
| 21 | + |
| 22 | + fixtures = ['tests.json'] |
| 23 | + |
| 24 | + def setUp(self): |
| 25 | + self.assertTrue( |
| 26 | + self.client.login(username='autotest', password='password')) |
| 27 | + url = '/api/providers' |
| 28 | + creds = {'secret_key': 'x' * 64, 'access_key': 1 * 20} |
| 29 | + body = {'id': 'autotest', 'type': 'mock', 'creds': json.dumps(creds)} |
| 30 | + response = self.client.post(url, json.dumps(body), content_type='application/json') |
| 31 | + self.assertEqual(response.status_code, 201) |
| 32 | + url = '/api/flavors' |
| 33 | + body = { |
| 34 | + 'id': 'autotest', |
| 35 | + 'provider': 'autotest', |
| 36 | + 'params': json.dumps({'region': 'us-west-2'}), |
| 37 | + } |
| 38 | + response = self.client.post(url, json.dumps(body), content_type='application/json') |
| 39 | + self.assertEqual(response.status_code, 201) |
| 40 | + response = self.client.post('/api/formations', json.dumps( |
| 41 | + {'id': 'autotest', 'domain': 'localhost.localdomain'}), |
| 42 | + content_type='application/json') |
| 43 | + self.assertEqual(response.status_code, 201) |
| 44 | + |
| 45 | + def test_push(self): |
| 46 | + """ |
| 47 | + Test that a user can push into the system |
| 48 | + """ |
| 49 | + url = '/api/apps' |
| 50 | + body = {'formation': 'autotest'} |
| 51 | + response = self.client.post(url, json.dumps(body), content_type='application/json') |
| 52 | + self.assertEqual(response.status_code, 201) |
| 53 | + app_id = response.data['id'] |
| 54 | + # prepare a push body |
| 55 | + body = { |
| 56 | + 'sha': 'df1e628f2244b73f9cdf944f880a2b3470a122f4', |
| 57 | + 'fingerprint': '88:25:ed:67:56:91:3d:c6:1b:7f:42:c6:9b:41:24:80', |
| 58 | + 'receive_user': 'autotest', |
| 59 | + 'receive_repo': 'repo.git', |
| 60 | + 'ssh_connection': '10.0.1.10 50337 172.17.0.143 22', |
| 61 | + 'ssh_original_command': "git-receive-pack 'repo.git'", |
| 62 | + } |
| 63 | + # post a request without the auth header |
| 64 | + url = "/api/apps/{app_id}/push".format(**locals()) |
| 65 | + response = self.client.post(url, json.dumps(body), content_type='application/json') |
| 66 | + self.assertEqual(response.status_code, 403) |
| 67 | + # now try with the builder key in the special auth header |
| 68 | + response = self.client.post(url, json.dumps(body), content_type='application/json', |
| 69 | + HTTP_X_DEIS_BUILDER_AUTH=settings.BUILDER_KEY) |
| 70 | + self.assertEqual(response.status_code, 201) |
| 71 | + for k in ('owner', 'app', 'sha', 'fingerprint', 'receive_repo', 'receive_user', |
| 72 | + 'ssh_connection', 'ssh_original_command'): |
| 73 | + self.assertIn(k, response.data) |
| 74 | + |
| 75 | + def test_push_abuse(self): |
| 76 | + # create a legit app |
| 77 | + url = '/api/apps' |
| 78 | + body = {'formation': 'autotest'} |
| 79 | + response = self.client.post(url, json.dumps(body), content_type='application/json') |
| 80 | + self.assertEqual(response.status_code, 201) |
| 81 | + app_id = response.data['id'] |
| 82 | + # register an evil user |
| 83 | + username, password = 'eviluser', 'password' |
| 84 | + first_name, last_name = 'Evil', 'User' |
| 85 | + email = 'evil@deis.io' |
| 86 | + submit = { |
| 87 | + 'username': username, |
| 88 | + 'password': password, |
| 89 | + 'first_name': first_name, |
| 90 | + 'last_name': last_name, |
| 91 | + 'email': email, |
| 92 | + } |
| 93 | + url = '/api/auth/register' |
| 94 | + response = self.client.post(url, json.dumps(submit), content_type='application/json') |
| 95 | + self.assertEqual(response.status_code, 201) |
| 96 | + # prepare a push body that simulates a git push |
| 97 | + body = { |
| 98 | + 'sha': 'df1e628f2244b73f9cdf944f880a2b3470a122f4', |
| 99 | + 'fingerprint': '88:25:ed:67:56:91:3d:c6:1b:7f:42:c6:9b:41:24:99', |
| 100 | + 'receive_user': 'eviluser', |
| 101 | + 'receive_repo': 'repo.git', |
| 102 | + 'ssh_connection': '10.0.1.10 50337 172.17.0.143 22', |
| 103 | + 'ssh_original_command': "git-receive-pack 'repo.git'", |
| 104 | + } |
| 105 | + # try to push as "eviluser" |
| 106 | + url = "/api/apps/{app_id}/push".format(**locals()) |
| 107 | + response = self.client.post(url, json.dumps(body), content_type='application/json', |
| 108 | + HTTP_X_DEIS_BUILDER_AUTH=settings.BUILDER_KEY) |
| 109 | + self.assertEqual(response.status_code, 403) |
0 commit comments