diff --git a/src/main/java/org/jenkinsci/plugins/GithubAuthenticationToken.java b/src/main/java/org/jenkinsci/plugins/GithubAuthenticationToken.java index e1e4562a..b8b3dcb4 100644 --- a/src/main/java/org/jenkinsci/plugins/GithubAuthenticationToken.java +++ b/src/main/java/org/jenkinsci/plugins/GithubAuthenticationToken.java @@ -469,7 +469,7 @@ GHUser loadUser(@NonNull String username) throws IOException { GithubUser user; try { user = usersByIdCache.getIfPresent(username); - if (gh != null && user == null && isAuthenticated()) { + if (user == null && isAuthenticated()) { GHUser ghUser = getGitHub().getUser(username); user = new GithubUser(ghUser); usersByIdCache.put(username, user); @@ -505,7 +505,7 @@ private GHMyself loadMyself(@NonNull String token) throws IOException { @Nullable GHOrganization loadOrganization(@NonNull String organization) { try { - if (gh != null && isAuthenticated()) + if (isAuthenticated()) return getGitHub().getOrganization(organization); } catch (IOException | RuntimeException e) { LOGGER.log(Level.FINEST, e.getMessage(), e); @@ -516,7 +516,7 @@ GHOrganization loadOrganization(@NonNull String organization) { @NonNull private RepoRights loadRepository(@NonNull final String repositoryName) { try { - if (gh != null && isAuthenticated() && (myRealm.hasScope("repo") || myRealm.hasScope("public_repo"))) { + if (isAuthenticated() && (myRealm.hasScope("repo") || myRealm.hasScope("public_repo"))) { Cache repoNameToRightsCache = myRepositories(); return repoNameToRightsCache.get(repositoryName, unused -> { GHRepository repo; diff --git a/src/test/java/org/jenkinsci/plugins/GithubAuthenticationTokenTest.java b/src/test/java/org/jenkinsci/plugins/GithubAuthenticationTokenTest.java index 1051e7b1..66a4ccf2 100644 --- a/src/test/java/org/jenkinsci/plugins/GithubAuthenticationTokenTest.java +++ b/src/test/java/org/jenkinsci/plugins/GithubAuthenticationTokenTest.java @@ -6,6 +6,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.kohsuke.github.GHMyself; +import org.kohsuke.github.GHUser; import org.kohsuke.github.GitHub; import org.kohsuke.github.GitHubBuilder; import org.kohsuke.github.RateLimitHandler; @@ -22,7 +23,7 @@ @ExtendWith(MockitoExtension.class) class GithubAuthenticationTokenTest { - @Mock(strictness = Mock.Strictness.LENIENT) + @Mock private GithubSecurityRealm securityRealm; @AfterEach @@ -34,7 +35,7 @@ private void mockJenkins(MockedStatic mockedJenkins) { Jenkins jenkins = Mockito.mock(Jenkins.class); mockedJenkins.when(Jenkins::get).thenReturn(jenkins); Mockito.when(jenkins.getSecurityRealm()).thenReturn(securityRealm); - Mockito.when(securityRealm.getOauthScopes()).thenReturn("read:org"); + Mockito.when(securityRealm.hasScope("read:org")).thenReturn(true); } @Test @@ -42,18 +43,34 @@ void testTokenSerialization() throws IOException { try (MockedStatic mockedJenkins = Mockito.mockStatic(Jenkins.class); MockedStatic mockedGitHubBuilder = Mockito.mockStatic(GitHubBuilder.class)) { mockJenkins(mockedJenkins); - mockGHMyselfAs(mockedGitHubBuilder, "bob"); + GitHub gh = mockGH(mockedGitHubBuilder); + mockGHMyselfAs(gh, "bob"); GithubAuthenticationToken authenticationToken = new GithubAuthenticationToken("accessToken", "https://api.github.com"); byte[] serializedToken = SerializationUtils.serialize(authenticationToken); GithubAuthenticationToken deserializedToken = (GithubAuthenticationToken) SerializationUtils.deserialize(serializedToken); assertEquals(deserializedToken.getAccessToken(), authenticationToken.getAccessToken()); assertEquals(deserializedToken.getPrincipal(), authenticationToken.getPrincipal()); assertEquals(deserializedToken.getGithubServer(), authenticationToken.getGithubServer()); - assertEquals(deserializedToken.getMyself().getLogin(), deserializedToken.getMyself().getLogin()); + assertEquals(deserializedToken.getMyself().getLogin(), authenticationToken.getMyself().getLogin()); } } - private static GHMyself mockGHMyselfAs(MockedStatic mockedGitHubBuilder, String username) throws IOException { + @Test + void testLoadUser() throws IOException { + try (MockedStatic mockedJenkins = Mockito.mockStatic(Jenkins.class); + MockedStatic mockedGitHubBuilder = Mockito.mockStatic(GitHubBuilder.class)) { + mockJenkins(mockedJenkins); + GitHub gh = mockGH(mockedGitHubBuilder); + mockGHMyselfAs(gh, "bob"); + mockGHUser(gh, "charlie"); + GithubAuthenticationToken authenticationToken = new GithubAuthenticationToken("accessToken", "https://api.github.com"); + byte[] serializedToken = SerializationUtils.serialize(authenticationToken); + GithubAuthenticationToken deserializedToken = (GithubAuthenticationToken) SerializationUtils.deserialize(serializedToken); + assertEquals(deserializedToken.loadUser("charlie"), authenticationToken.loadUser("charlie")); + } + } + + private static GitHub mockGH(MockedStatic mockedGitHubBuilder) throws IOException { GitHub gh = Mockito.mock(GitHub.class); GitHubBuilder builder = Mockito.mock(GitHubBuilder.class); mockedGitHubBuilder.when(GitHubBuilder::fromEnvironment).thenReturn(builder); @@ -62,9 +79,19 @@ private static GHMyself mockGHMyselfAs(MockedStatic mockedGitHubB Mockito.when(builder.withRateLimitHandler(RateLimitHandler.FAIL)).thenReturn(builder); Mockito.when(builder.withConnector(Mockito.any(OkHttpGitHubConnector.class))).thenReturn(builder); Mockito.when(builder.build()).thenReturn(gh); + return gh; + } + + private static GHMyself mockGHMyselfAs(GitHub gh, String username) throws IOException { GHMyself me = Mockito.mock(GHMyself.class); Mockito.when(gh.getMyself()).thenReturn(me); Mockito.when(me.getLogin()).thenReturn(username); return me; } + + private static GHUser mockGHUser(GitHub gh, String username) throws IOException { + GHUser user = Mockito.mock(GHUser.class); + Mockito.when(gh.getUser(username)).thenReturn(user); + return user; + } }