77from __future__ import unicode_literals
88
99import json
10+ import uuid
1011
1112from django .test import TestCase
1213from django .test .utils import override_settings
1516
1617
1718@override_settings (CELERY_ALWAYS_EAGER = True )
18- class PushTest (TestCase ):
19+ class HookTest (TestCase ):
1920
20- """Tests pushes into the push system """
21+ """Tests API hooks used to trigger actions from external components """
2122
2223 fixtures = ['tests.json' ]
2324
@@ -42,10 +43,8 @@ def setUp(self):
4243 content_type = 'application/json' )
4344 self .assertEqual (response .status_code , 201 )
4445
45- def test_push (self ):
46- """
47- Test that a user can push into the system
48- """
46+ def test_push_hook (self ):
47+ """Test creating a Push via the API"""
4948 url = '/api/apps'
5049 body = {'formation' : 'autotest' }
5150 response = self .client .post (url , json .dumps (body ), content_type = 'application/json' )
@@ -56,12 +55,12 @@ def test_push(self):
5655 'sha' : 'df1e628f2244b73f9cdf944f880a2b3470a122f4' ,
5756 'fingerprint' : '88:25:ed:67:56:91:3d:c6:1b:7f:42:c6:9b:41:24:80' ,
5857 'receive_user' : 'autotest' ,
59- 'receive_repo' : 'repo.git' ,
58+ 'receive_repo' : '{app_id}' . format ( ** locals ()) ,
6059 'ssh_connection' : '10.0.1.10 50337 172.17.0.143 22' ,
61- 'ssh_original_command' : "git-receive-pack 'repo .git'" ,
60+ 'ssh_original_command' : "git-receive-pack '{app_id} .git'" . format ( ** locals ()) ,
6261 }
6362 # post a request without the auth header
64- url = "/api/apps/{app_id} /push" .format (** locals ())
63+ url = "/api/hooks /push" .format (** locals ())
6564 response = self .client .post (url , json .dumps (body ), content_type = 'application/json' )
6665 self .assertEqual (response .status_code , 403 )
6766 # now try with the builder key in the special auth header
@@ -73,7 +72,8 @@ def test_push(self):
7372 self .assertIn (k , response .data )
7473
7574 def test_push_abuse (self ):
76- # create a legit app
75+ """Test a user pushing to an unauthorized application"""
76+ # create a legit app as "autotest"
7777 url = '/api/apps'
7878 body = {'formation' : 'autotest' }
7979 response = self .client .post (url , json .dumps (body ), content_type = 'application/json' )
@@ -98,12 +98,68 @@ def test_push_abuse(self):
9898 'sha' : 'df1e628f2244b73f9cdf944f880a2b3470a122f4' ,
9999 'fingerprint' : '88:25:ed:67:56:91:3d:c6:1b:7f:42:c6:9b:41:24:99' ,
100100 'receive_user' : 'eviluser' ,
101- 'receive_repo' : 'repo.git' ,
101+ 'receive_repo' : '{app_id}' . format ( ** locals ()) ,
102102 'ssh_connection' : '10.0.1.10 50337 172.17.0.143 22' ,
103- 'ssh_original_command' : "git-receive-pack 'repo .git'" ,
103+ 'ssh_original_command' : "git-receive-pack '{app_id} .git'" . format ( ** locals ()) ,
104104 }
105105 # try to push as "eviluser"
106- url = "/api/apps/{app_id} /push" .format (** locals ())
106+ url = "/api/hooks /push" .format (** locals ())
107107 response = self .client .post (url , json .dumps (body ), content_type = 'application/json' ,
108108 HTTP_X_DEIS_BUILDER_AUTH = settings .BUILDER_KEY )
109109 self .assertEqual (response .status_code , 403 )
110+
111+ def test_build_hook (self ):
112+ """Test creating a Build via the API"""
113+ formation_id = 'autotest'
114+ url = '/api/formations/{formation_id}/layers' .format (** locals ())
115+ body = {'id' : 'runtime' , 'flavor' : 'autotest' , 'runtime' : True , 'proxy' : True }
116+ response = self .client .post (url , json .dumps (body ), content_type = 'application/json' )
117+ self .assertEqual (response .status_code , 201 )
118+ url = '/api/formations/{formation_id}/scale' .format (** locals ())
119+ body = {'runtime' : 2 }
120+ response = self .client .post (url , json .dumps (body ), content_type = 'application/json' )
121+ self .assertEqual (response .status_code , 200 )
122+ url = '/api/apps'
123+ body = {'formation' : formation_id }
124+ response = self .client .post (url , json .dumps (body ), content_type = 'application/json' )
125+ self .assertEqual (response .status_code , 201 )
126+ app_id = response .data ['id' ]
127+ build = {'username' : 'autotest' , 'app' : app_id }
128+ url = '/api/hooks/builds' .format (** locals ())
129+ sha , checksum = uuid .uuid4 ().hex , uuid .uuid4 ().hex
130+ body = {'receive_user' : 'autotest' ,
131+ 'receive_repo' : app_id ,
132+ 'sha' : sha ,
133+ 'checksum' : checksum ,
134+ 'procfile' : {'web' : 'node server.js' },
135+ 'config' : {'PATH' : '/usr/local/bin:/usr/bin:/usr/sbin' },
136+ 'url' : 'http://deis-controller.local/slugs/{app_id}-{sha}.tar.gz' .format (** locals ()),
137+ 'size' : 12345 }
138+ # post the build without a session
139+ self .assertIsNone (self .client .logout ())
140+ response = self .client .post (url , json .dumps (body ), content_type = 'application/json' )
141+ self .assertEqual (response .status_code , 403 )
142+ # post the build with the builder auth key
143+ response = self .client .post (url , json .dumps (body ), content_type = 'application/json' ,
144+ HTTP_X_DEIS_BUILDER_AUTH = settings .BUILDER_KEY )
145+ self .assertEqual (response .status_code , 201 )
146+ build = response .data
147+ self .assertIn ('sha' , response .data )
148+ self .assertIn ('procfile' , response .data )
149+ procfile = json .loads (response .data ['procfile' ])
150+ self .assertIn ('web' , procfile )
151+ self .assertEqual (procfile ['web' ], 'node server.js' )
152+ # calculate the databag
153+ self .assertTrue (
154+ self .client .login (username = 'autotest' , password = 'password' ))
155+ url = '/api/apps/{app_id}/calculate' .format (** locals ())
156+ response = self .client .post (url )
157+ self .assertEqual (response .status_code , 200 )
158+ databag = response .data
159+ self .assertIn ('release' , databag )
160+ self .assertIn ('version' , databag ['release' ])
161+ self .assertIn ('containers' , databag )
162+ self .assertIn ('web' , databag ['containers' ])
163+ self .assertIn ('1' , databag ['containers' ]['web' ])
164+ self .assertEqual (databag ['containers' ]['web' ]['1' ], 'up' )
165+
0 commit comments