Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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<String, RepoRights> repoNameToRightsCache = myRepositories();
return repoNameToRightsCache.get(repositoryName, unused -> {
GHRepository repo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,7 +23,7 @@
@ExtendWith(MockitoExtension.class)
class GithubAuthenticationTokenTest {

@Mock(strictness = Mock.Strictness.LENIENT)
@Mock
private GithubSecurityRealm securityRealm;

@AfterEach
Expand All @@ -34,26 +35,42 @@ private void mockJenkins(MockedStatic<Jenkins> 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
void testTokenSerialization() throws IOException {
try (MockedStatic<Jenkins> mockedJenkins = Mockito.mockStatic(Jenkins.class);
MockedStatic<GitHubBuilder> 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<GitHubBuilder> mockedGitHubBuilder, String username) throws IOException {
@Test
void testLoadUser() throws IOException {
try (MockedStatic<Jenkins> mockedJenkins = Mockito.mockStatic(Jenkins.class);
MockedStatic<GitHubBuilder> 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<GitHubBuilder> mockedGitHubBuilder) throws IOException {
GitHub gh = Mockito.mock(GitHub.class);
GitHubBuilder builder = Mockito.mock(GitHubBuilder.class);
mockedGitHubBuilder.when(GitHubBuilder::fromEnvironment).thenReturn(builder);
Expand All @@ -62,9 +79,19 @@ private static GHMyself mockGHMyselfAs(MockedStatic<GitHubBuilder> 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;
}
}