77import java .util .ArrayList ;
88import java .util .List ;
99
10- import com .larscheidschmitzhermes .nexus3 .github .oauth .plugin .configuration .GithubOauthConfiguration ;
1110import org .apache .http .HttpResponse ;
1211import org .apache .http .client .HttpClient ;
1312import org .apache .http .client .methods .HttpGet ;
2322import com .larscheidschmitzhermes .nexus3 .github .oauth .plugin .api .GithubOrg ;
2423import com .larscheidschmitzhermes .nexus3 .github .oauth .plugin .api .GithubTeam ;
2524import com .larscheidschmitzhermes .nexus3 .github .oauth .plugin .api .GithubUser ;
25+ import com .larscheidschmitzhermes .nexus3 .github .oauth .plugin .configuration .GithubOauthConfiguration ;
2626import com .larscheidschmitzhermes .nexus3 .github .oauth .plugin .configuration .MockGithubOauthConfiguration ;
2727
2828@ RunWith (MockitoJUnitRunner .class )
2929public class GithubApiClientTest {
3030 private MockGithubOauthConfiguration config = new MockGithubOauthConfiguration (Duration .ofDays (1 ));
3131 private ObjectMapper mapper = new ObjectMapper ();
3232
33- private List <GithubOrg > mockOrgs (){
34- List <GithubOrg > orgs = new ArrayList <>();
33+ private List <GithubTeam > mockTeams (){
3534 GithubOrg org = new GithubOrg ();
3635 org .setLogin ("TEST-ORG" );
37- org .setUrl ("www.example.com/test-org" );
38- orgs .add (org );
39- return orgs ;
40- }
4136
42- private List <GithubTeam > mockTeams (){
4337 List <GithubTeam > teams = new ArrayList <>();
38+
4439 GithubTeam team = new GithubTeam ();
40+ team .setOrganization (org );
4541 team .setName ("admin" );
4642 teams .add (team );
43+
4744 return teams ;
4845 }
4946
@@ -70,17 +67,14 @@ private HttpClient fullyFunctionalMockClient() throws IOException{
7067 return mockClient ;
7168 }
7269 private void mockResponsesForGithubAuthRequest (HttpClient mockClient ) throws IOException {
73- HttpResponse mockOrgRespone = createMockResponse (mockOrgs ());
7470 HttpResponse mockTeamResponse = createMockResponse (mockTeams ());
7571 HttpResponse mockUserResponse = createMockResponse (mockUser ("Hans Wurst" ));
7672
7773 Mockito .when (mockClient .execute (Mockito .any ())).thenAnswer (invocationOnMock -> {
7874 String uriString = ((HttpGet ) invocationOnMock .getArguments ()[0 ]).getURI ().toString ();
79- if (uriString .equals (config .getGithubOrgsUri ().toString ())){
80- return mockOrgRespone ;
81- }else if (uriString .matches (".*/teams.*" )){
75+ if (uriString .equals (config .getGithubUserTeamsUri ())) {
8276 return mockTeamResponse ;
83- }else if (uriString .equals (config .getGithubUserUri ().toString ())){
77+ } else if (uriString .equals (config .getGithubUserUri ().toString ())) {
8478 return mockUserResponse ;
8579 }
8680 return null ;
@@ -89,17 +83,14 @@ private void mockResponsesForGithubAuthRequest(HttpClient mockClient) throws IOE
8983
9084 private HttpClient mockClientWithNullUsername ()throws IOException {
9185 HttpClient mockClient = Mockito .mock (HttpClient .class );
92- HttpResponse mockOrgRespone = createMockResponse (mockOrgs ());
9386 HttpResponse mockTeamResponse = createMockResponse (mockTeams ());
9487 HttpResponse mockUserResponse = createMockResponse (mockUser (null ));
9588
9689 Mockito .when (mockClient .execute (Mockito .any ())).thenAnswer (invocationOnMock -> {
9790 String uriString = ((HttpGet ) invocationOnMock .getArguments ()[0 ]).getURI ().toString ();
98- if (uriString .equals (config .getGithubOrgsUri ().toString ())){
99- return mockOrgRespone ;
100- }else if (uriString .matches (".*/teams.*" )){
91+ if (uriString .equals (config .getGithubUserTeamsUri ())) {
10192 return mockTeamResponse ;
102- }else if (uriString .equals (config .getGithubUserUri ().toString ())){
93+ } else if (uriString .equals (config .getGithubUserUri ().toString ())){
10394 return mockUserResponse ;
10495 }
10596 return null ;
@@ -161,8 +152,8 @@ public void cachedPrincipalReturnsIfNotExpired() throws Exception {
161152 char [] token = "DUMMY" .toCharArray ();
162153 clientToTest .authz (login , token );
163154
164- // We make 3 calls to Github for a single auth check
165- Mockito .verify (mockClient , Mockito .times (3 )).execute (Mockito .any (HttpGet .class ));
155+ // We make 2 calls to Github for a single auth check
156+ Mockito .verify (mockClient , Mockito .times (2 )).execute (Mockito .any (HttpGet .class ));
166157 Mockito .verifyNoMoreInteractions (mockClient );
167158
168159 // This invocation should hit the cache and should not use the client
@@ -179,8 +170,8 @@ public void principalCacheHonorsTtl() throws Exception {
179170 char [] token = "DUMMY" .toCharArray ();
180171
181172 clientToTest .authz ("demo-user" , token );
182- // We make 3 calls to Github for a single auth check
183- Mockito .verify (mockClient , Mockito .times (3 )).execute (Mockito .any (HttpGet .class ));
173+ // We make 2 calls to Github for a single auth check
174+ Mockito .verify (mockClient , Mockito .times (2 )).execute (Mockito .any (HttpGet .class ));
184175 Mockito .verifyNoMoreInteractions (mockClient );
185176
186177 // Wait a bit for the cache to become invalidated
@@ -191,8 +182,8 @@ public void principalCacheHonorsTtl() throws Exception {
191182 mockResponsesForGithubAuthRequest (mockClient );
192183 // This should also hit Github because the cache TTL has elapsed
193184 clientToTest .authz ("demo-user" , token );
194- // We make 3 calls to Github for a single auth check
195- Mockito .verify (mockClient , Mockito .times (3 )).execute (Mockito .any (HttpGet .class ));
185+ // We make 2 calls to Github for a single auth check
186+ Mockito .verify (mockClient , Mockito .times (2 )).execute (Mockito .any (HttpGet .class ));
196187 Mockito .verifyNoMoreInteractions (mockClient );
197188 }
198189
0 commit comments