1919
2020import com .sun .jersey .api .client .ClientResponse ;
2121import com .sun .jersey .core .util .MultivaluedMapImpl ;
22+ import com .yammer .dropwizard .auth .basic .BasicAuthProvider ;
2223import com .yammer .dropwizard .testing .ResourceTest ;
24+ import org .apache .commons .codec .binary .Base64 ;
2325import org .junit .Test ;
26+ import org .whispersystems .bithub .auth .GithubWebhookAuthenticator ;
2427import org .whispersystems .bithub .client .CoinbaseClient ;
2528import org .whispersystems .bithub .client .GithubClient ;
2629import org .whispersystems .bithub .client .TransferFailedException ;
@@ -47,6 +50,14 @@ public class GithubControllerTest extends ResourceTest {
4750 private final CoinbaseClient coinbaseClient = mock (CoinbaseClient .class );
4851 private final GithubClient githubClient = mock (GithubClient .class );
4952
53+ // HTTP Basic Authentication data
54+ private final String authUsername = "TestUser" ;
55+ private final String authPassword = "TestPassword" ;
56+ private final String authRealm = GithubWebhookAuthenticator .REALM ;
57+ private final String authString = "Basic " + Base64 .encodeBase64String ((authUsername + ":" + authPassword ).getBytes ());
58+ private final String invalidUserAuthString = "Basic " + Base64 .encodeBase64 (("wrong:" + authPassword ).getBytes ());
59+ private final String invalidPasswordAuthString = "Basic " + Base64 .encodeBase64 ((authUsername + ":wrong" ).getBytes ());
60+
5061 private final List <String > repositories = new LinkedList <String >() {{
5162 add ("https://github.com/moxie0/test" );
5263 }};
@@ -57,6 +68,7 @@ protected void setUpResources() throws Exception {
5768 when (coinbaseClient .getExchangeRate ()).thenReturn (EXCHANGE_RATE );
5869 addResource (new GithubController (repositories , githubClient , coinbaseClient , new BigDecimal (0.02 )));
5970 addProvider (new UnauthorizedHookExceptionMapper ());
71+ addProvider (new BasicAuthProvider <>(new GithubWebhookAuthenticator (authUsername , authPassword ), authRealm ));
6072 }
6173
6274 protected String payload (String path ) {
@@ -72,6 +84,7 @@ public void testInvalidRepository() throws Exception {
7284 post .add ("payload" , payloadValue );
7385 ClientResponse response = client ().resource ("/v1/github/commits/" )
7486 .header ("X-Forwarded-For" , "192.30.252.1" )
87+ .header ("Authorization" , authString )
7588 .type (MediaType .APPLICATION_FORM_URLENCODED_TYPE )
7689 .post (ClientResponse .class , post );
7790
@@ -85,19 +98,62 @@ public void testInvalidOrigin() throws Exception {
8598 post .add ("payload" , payloadValue );
8699 ClientResponse response = client ().resource ("/v1/github/commits/" )
87100 .header ("X-Forwarded-For" , "192.30.242.1" )
101+ .header ("Authorization" , authString )
88102 .type (MediaType .APPLICATION_FORM_URLENCODED_TYPE )
89103 .post (ClientResponse .class , post );
90104
91105 assertThat (response .getStatus ()).isEqualTo (401 );
92106 }
93107
108+ @ Test
109+ public void testMissingAuth () throws Exception , TransferFailedException {
110+ String payloadValue = payload ("/payloads/valid_commit.json" );
111+ MultivaluedMapImpl post = new MultivaluedMapImpl ();
112+ post .add ("payload" , payloadValue );
113+ ClientResponse response = client ().resource ("/v1/github/commits/" )
114+ .header ("X-Forwarded-For" , "192.30.252.1" )
115+ .type (MediaType .APPLICATION_FORM_URLENCODED_TYPE )
116+ .post (ClientResponse .class , post );
117+
118+ assertThat (response .getStatus ()).isEqualTo (401 );
119+ }
120+
121+ @ Test
122+ public void testInvalidAuthUser () throws Exception , TransferFailedException {
123+ String payloadValue = payload ("/payloads/valid_commit.json" );
124+ MultivaluedMapImpl post = new MultivaluedMapImpl ();
125+ post .add ("payload" , payloadValue );
126+ ClientResponse response = client ().resource ("/v1/github/commits/" )
127+ .header ("X-Forwarded-For" , "192.30.252.1" )
128+ .header ("Authorization" , invalidUserAuthString )
129+ .type (MediaType .APPLICATION_FORM_URLENCODED_TYPE )
130+ .post (ClientResponse .class , post );
131+
132+ assertThat (response .getStatus ()).isEqualTo (401 );
133+ }
134+
135+ @ Test
136+ public void testInvalidAuthPassword () throws Exception , TransferFailedException {
137+ String payloadValue = payload ("/payloads/valid_commit.json" );
138+ MultivaluedMapImpl post = new MultivaluedMapImpl ();
139+ post .add ("payload" , payloadValue );
140+ ClientResponse response = client ().resource ("/v1/github/commits/" )
141+ .header ("X-Forwarded-For" , "192.30.252.1" )
142+ .header ("Authorization" , invalidPasswordAuthString )
143+ .type (MediaType .APPLICATION_FORM_URLENCODED_TYPE )
144+ .post (ClientResponse .class , post );
145+
146+ assertThat (response .getStatus ()).isEqualTo (401 );
147+ }
148+
94149 @ Test
95150 public void testOptOutCommit () throws Exception , TransferFailedException {
96151 String payloadValue = payload ("/payloads/opt_out_commit.json" );
97152 MultivaluedMapImpl post = new MultivaluedMapImpl ();
98153 post .add ("payload" , payloadValue );
99154 ClientResponse response = client ().resource ("/v1/github/commits/" )
100155 .header ("X-Forwarded-For" , "192.30.252.1" )
156+ .header ("Authorization" , authString )
101157 .type (MediaType .APPLICATION_FORM_URLENCODED_TYPE )
102158 .post (ClientResponse .class , post );
103159
@@ -113,6 +169,7 @@ public void testValidCommit() throws Exception, TransferFailedException {
113169 post .add ("payload" , payloadValue );
114170 ClientResponse response = client ().resource ("/v1/github/commits/" )
115171 .header ("X-Forwarded-For" , "192.30.252.1" )
172+ .header ("Authorization" , authString )
116173 .type (MediaType .APPLICATION_FORM_URLENCODED_TYPE )
117174 .post (ClientResponse .class , post );
118175
@@ -126,8 +183,11 @@ public void testValidMultipleCommitsMultipleAuthors() throws Exception, Transfer
126183 String payloadValue = payload ("/payloads/multiple_commits_authors.json" );
127184 MultivaluedMapImpl post = new MultivaluedMapImpl ();
128185 post .add ("payload" , payloadValue );
129- ClientResponse response = client ().resource ("/v1/github/commits/" ).header ("X-Forwarded-For" , "192.30.252.1" )
130- .type (MediaType .APPLICATION_FORM_URLENCODED_TYPE ).post (ClientResponse .class , post );
186+ ClientResponse response = client ().resource ("/v1/github/commits/" )
187+ .header ("X-Forwarded-For" , "192.30.252.1" )
188+ .header ("Authorization" , authString )
189+ .type (MediaType .APPLICATION_FORM_URLENCODED_TYPE )
190+ .post (ClientResponse .class , post );
131191
132192 verify (coinbaseClient , times (1 )).sendPayment (any (Author .class ), eq (BALANCE .multiply (new BigDecimal (0.02 ))),
133193 anyString ());
0 commit comments