1515
1616from . import mock_status_ok
1717
18+ RSA_PUBKEY = (
19+ "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCfQkkUUoxpvcNMkvv7jqnfodgs37M2eBO"
20+ "APgLK+KNBMaZaaKB4GF1QhTCMfFhoiTW3rqa0J75bHJcdkoobtTHlK8XUrFqsquWyg3XhsT"
21+ "Yr/3RQQXvO86e2sF7SVDJqVtpnbQGc5SgNrHCeHJmf5HTbXSIjCO/AJSvIjnituT/SIAMGe"
22+ "Bw0Nq/iSltwYAek1hiKO7wSmLcIQ8U4A00KEUtalaumf2aHOcfjgPfzlbZGP0S0cuBwSqLr"
23+ "8b5XGPmkASNdUiuJY4MJOce7bFU14B7oMAy2xacODUs1momUeYtGI9T7X2WMowJaO7tP3Gl"
24+ "sgBMP81VfYTfYChAyJpKp2yoP autotest@autotesting comment"
25+ )
26+
27+ RSA_PUBKEY2 = (
28+ "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC4xELdubosJ2/bQuiSUyWclVVa71pXpmq"
29+ "aXTwfau/XFLgD5yE+TOFbVT22xvEr4AwZqS9w0TBMp4RLfi4pTdjoIK+lau2lDMuEpbF4xg"
30+ "PWAveAqKuLcKJbJrZQdo5VWn5//7+M1RHQCPqjeN2iS9I3C8yiPg3mMPT2mKuyZYB9VD3hK"
31+ "mhT4xRAsS6vfKZr7CmFHgAmRBqdaU1RetR5nfTj0R5yyAv7Z2BkE8UhUAseFZ0djBs6kzjs"
32+ "5ddgM4Gv2Zajs7qVvpVPzZpq3vFB16Q5TMj2YtoYF6UZFFf4u/4KAW8xfYJAFdpNsvh279s"
33+ "dJS08nTeElUg6pn83A3hqWX+J testing"
34+ )
35+
1836
1937@mock .patch ('api.models.release.publish_release' , lambda * args : None )
2038class HookTest (TransactionTestCase ):
@@ -27,12 +45,67 @@ def setUp(self):
2745 self .user = User .objects .get (username = 'autotest' )
2846 self .token = Token .objects .get (user = self .user ).key
2947
48+ def test_key_hook (self ):
49+ """Test fetching keys for an app and a user"""
50+
51+ # Create app to use
52+ url = '/v2/apps'
53+ response = self .client .post (url , HTTP_AUTHORIZATION = 'token {}' .format (self .token ))
54+ self .assertEqual (response .status_code , 201 )
55+ app_id = response .data ['id' ]
56+
57+ # give user permission to app
58+ url = "/v2/apps/{}/perms" .format (app_id )
59+ body = {'username' : str (self .user )}
60+ response = self .client .post (url , json .dumps (body ), content_type = 'application/json' ,
61+ HTTP_AUTHORIZATION = 'token {}' .format (self .token ))
62+ self .assertEqual (response .status_code , 201 )
63+
64+ # Create key
65+ url = '/v2/keys'
66+ body = {'id' : str (self .user ), 'public' : RSA_PUBKEY }
67+ response = self .client .post (url , json .dumps (body ), content_type = 'application/json' ,
68+ HTTP_AUTHORIZATION = 'token {}' .format (self .token ))
69+ self .assertEqual (response .status_code , 201 )
70+ public = response .data ['public' ]
71+
72+ # Create another keys
73+ url = '/v2/keys'
74+ body = {'id' : str (self .user ), 'public' : RSA_PUBKEY2 }
75+ response = self .client .post (url , json .dumps (body ), content_type = 'application/json' ,
76+ HTTP_AUTHORIZATION = 'token {}' .format (self .token ))
77+ self .assertEqual (response .status_code , 201 )
78+ public2 = response .data ['public' ]
79+
80+ # Make sure 404 is returned for a random app
81+ url = '/v2/hooks/keys/doesnotexist'
82+ response = self .client .get (url , HTTP_X_DEIS_BUILDER_AUTH = settings .BUILDER_KEY )
83+ self .assertEqual (response .status_code , 404 )
84+
85+ # Test app that exists
86+ url = '/v2/hooks/keys/{}' .format (app_id )
87+ response = self .client .get (url , HTTP_X_DEIS_BUILDER_AUTH = settings .BUILDER_KEY )
88+ self .assertEqual (response .status_code , 200 )
89+ self .assertEqual (response .data , {"autotest" : [public , public2 ]})
90+
91+ # Test against an app that exist but user does not
92+ url = '/v2/hooks/keys/{}/foooooo' .format (app_id )
93+ response = self .client .get (url , HTTP_X_DEIS_BUILDER_AUTH = settings .BUILDER_KEY )
94+ self .assertEqual (response .status_code , 404 )
95+
96+ # Test against an app that exists and user that does
97+ url = '/v2/hooks/keys/{}/{}' .format (app_id , str (self .user ))
98+ response = self .client .get (url , HTTP_X_DEIS_BUILDER_AUTH = settings .BUILDER_KEY )
99+ self .assertEqual (response .status_code , 200 )
100+ self .assertEqual (response .data , {"autotest" : [public , public2 ]})
101+
30102 def test_push_hook (self ):
31103 """Test creating a Push via the API"""
32104 url = '/v2/apps'
33105 response = self .client .post (url , HTTP_AUTHORIZATION = 'token {}' .format (self .token ))
34106 self .assertEqual (response .status_code , 201 )
35107 app_id = response .data ['id' ]
108+
36109 # prepare a push body
37110 body = {
38111 'sha' : 'df1e628f2244b73f9cdf944f880a2b3470a122f4' ,
0 commit comments