11package com .google .jenkins .plugins .k8sengine ;
22
3- import static org .junit .Assert .assertEquals ;
43import static org .junit .Assert .assertNotNull ;
5- import static org .mockito .ArgumentMatchers .any ;
64
75import com .cloudbees .plugins .credentials .CredentialsStore ;
6+ import com .cloudbees .plugins .credentials .SecretBytes ;
87import com .cloudbees .plugins .credentials .SystemCredentialsProvider ;
98import com .cloudbees .plugins .credentials .domains .Domain ;
109import com .cloudbees .plugins .credentials .domains .DomainRequirement ;
1110import com .google .api .client .auth .oauth2 .Credential ;
1211import com .google .common .collect .ImmutableList ;
1312import com .google .jenkins .plugins .credentials .oauth .GoogleRobotCredentials ;
14- import com .google .jenkins .plugins .k8sengine .client .ContainerScopeRequirement ;
13+ import com .google .jenkins .plugins .credentials .oauth .GoogleRobotPrivateKeyCredentials ;
14+ import com .google .jenkins .plugins .credentials .oauth .JsonServiceAccountConfig ;
1515import hudson .AbortException ;
1616import java .io .IOException ;
17- import java .security .GeneralSecurityException ;
18- import jenkins .model .Jenkins ;
19- import org .junit .BeforeClass ;
17+ import java .nio .charset .StandardCharsets ;
2018import org .junit .ClassRule ;
2119import org .junit .Test ;
22- import org .junit .runner .RunWith ;
2320import org .jvnet .hudson .test .JenkinsRule ;
24- import org .mockito .Mockito ;
25- import org .mockito .junit .MockitoJUnitRunner ;
2621
27- @ RunWith (MockitoJUnitRunner .class )
2822public class CredentialsUtilTest {
2923 private static final String TEST_CREDENTIALS_ID = "test-credentials-id" ;
3024 private static final String TEST_INVALID_CREDENTIALS_ID = "test-invalid-credentials-id" ;
3125 private static final String TEST_ACCESS_TOKEN = "test-access-token" ;
3226 @ ClassRule public static JenkinsRule r = new JenkinsRule ();
33- public static Jenkins jenkins ;
34-
35- @ BeforeClass
36- public static void init () throws IOException {
37- jenkins = r .jenkins ;
38-
39- CredentialsStore store = new SystemCredentialsProvider .ProviderImpl ().getStore (jenkins );
40- GoogleRobotCredentials credentials = Mockito .mock (GoogleRobotCredentials .class );
41- Mockito .when (credentials .getId ()).thenReturn (TEST_CREDENTIALS_ID );
42- store .addCredentials (Domain .global (), credentials );
43- }
44-
45- @ Test
46- public void testGetRobotCredentialsReturnsFirstCredential () throws IOException {
47- assertNotNull (
48- CredentialsUtil .getRobotCredentials (
49- jenkins .get (), ImmutableList .<DomainRequirement >of (), TEST_CREDENTIALS_ID ));
50- }
5127
5228 @ Test (expected = AbortException .class )
5329 public void testGetRobotCredentialsInvalidCredentialsIdAbortException () throws AbortException {
5430 CredentialsUtil .getRobotCredentials (
55- jenkins . get () , ImmutableList .<DomainRequirement >of (), TEST_INVALID_CREDENTIALS_ID );
31+ r . jenkins , ImmutableList .<DomainRequirement >of (), TEST_INVALID_CREDENTIALS_ID );
5632 }
5733
58- @ Test (expected = AbortException .class )
59- public void testGetGoogleCredentialAbortException ()
60- throws GeneralSecurityException , AbortException {
61- GoogleRobotCredentials robotCreds = Mockito .mock (GoogleRobotCredentials .class );
62- Mockito .when (robotCreds .getGoogleCredential (any (ContainerScopeRequirement .class )))
63- .thenThrow (new GeneralSecurityException ());
34+ @ Test (expected = GoogleRobotPrivateKeyCredentials .PrivateKeyNotSetException .class )
35+ public void testGetGoogleCredentialAbortException () throws Exception {
36+ SecretBytes bytes =
37+ SecretBytes .fromBytes (
38+ "{\" client_email\" : \" [email protected] \" }" .
getBytes (
StandardCharsets .
UTF_8 ));
39+ JsonServiceAccountConfig serviceAccountConfig = new JsonServiceAccountConfig ();
40+ serviceAccountConfig .setSecretJsonKey (bytes );
41+ assertNotNull (serviceAccountConfig .getAccountId ());
42+ GoogleRobotCredentials robotCreds =
43+ new GoogleRobotPrivateKeyCredentials (
44+ TEST_INVALID_CREDENTIALS_ID , serviceAccountConfig , null );
45+ CredentialsStore store = new SystemCredentialsProvider .ProviderImpl ().getStore (r .jenkins );
46+ store .addCredentials (Domain .global (), robotCreds );
6447 CredentialsUtil .getGoogleCredential (robotCreds );
6548 }
6649
67- @ Test
68- public void testGetGoogleCredentialReturnsCredential ()
69- throws GeneralSecurityException , AbortException {
70- GoogleRobotCredentials robotCreds = Mockito .mock (GoogleRobotCredentials .class );
71- Credential credential = Mockito .mock (Credential .class );
72- Mockito .when (robotCreds .getGoogleCredential (any (ContainerScopeRequirement .class )))
73- .thenReturn (credential );
74- assertNotNull (CredentialsUtil .getGoogleCredential (robotCreds ));
75- }
76-
77- @ Test (expected = IOException .class )
78- public void testGetAccessTokenIOException () throws IOException {
79- Credential googleCredential = Mockito .mock (Credential .class );
80- Mockito .when (googleCredential .refreshToken ()).thenThrow (IOException .class );
81- CredentialsUtil .getAccessToken (googleCredential );
82- }
83-
84- @ Test
85- public void testGetAccessTokenReturnsToken () throws IOException {
86- Credential googleCredential = Mockito .mock (Credential .class );
87- Mockito .when (googleCredential .refreshToken ()).thenReturn (true );
88- Mockito .when (googleCredential .getAccessToken ()).thenReturn (TEST_ACCESS_TOKEN );
89- String accessToken = CredentialsUtil .getAccessToken (googleCredential );
90- assertNotNull (accessToken );
91- assertEquals (TEST_ACCESS_TOKEN , accessToken );
92- }
93-
9450 @ Test (expected = NullPointerException .class )
9551 public void testGetRobotCredentialsWithEmptyItemGroup () throws AbortException {
9652 CredentialsUtil .getRobotCredentials (
@@ -99,17 +55,17 @@ public void testGetRobotCredentialsWithEmptyItemGroup() throws AbortException {
9955
10056 @ Test (expected = NullPointerException .class )
10157 public void testGetRobotCredentialsWithEmptyDomainRequirements () throws AbortException {
102- CredentialsUtil .getRobotCredentials (jenkins . get () , null , TEST_CREDENTIALS_ID );
58+ CredentialsUtil .getRobotCredentials (r . jenkins , null , TEST_CREDENTIALS_ID );
10359 }
10460
10561 @ Test (expected = IllegalArgumentException .class )
10662 public void testGetRobotCredentialsWithNullCredentialsId () throws AbortException {
107- CredentialsUtil .getRobotCredentials (jenkins . get () , ImmutableList .<DomainRequirement >of (), null );
63+ CredentialsUtil .getRobotCredentials (r . jenkins , ImmutableList .<DomainRequirement >of (), null );
10864 }
10965
11066 @ Test (expected = IllegalArgumentException .class )
11167 public void testGetRobotCredentialsWithEmptyCredentialsId () throws AbortException {
112- CredentialsUtil .getRobotCredentials (jenkins . get () , ImmutableList .<DomainRequirement >of (), "" );
68+ CredentialsUtil .getRobotCredentials (r . jenkins , ImmutableList .<DomainRequirement >of (), "" );
11369 }
11470
11571 @ Test (expected = IllegalArgumentException .class )
0 commit comments