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 @@
GithubUser user;
try {
user = usersByIdCache.getIfPresent(username);
if (gh != null && user == null && isAuthenticated()) {
if (user == null && isAuthenticated()) {

Check warning on line 472 in src/main/java/org/jenkinsci/plugins/GithubAuthenticationToken.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 472 is only partially covered, one branch is missing
GHUser ghUser = getGitHub().getUser(username);
user = new GithubUser(ghUser);
usersByIdCache.put(username, user);
Expand Down Expand Up @@ -505,7 +505,7 @@
@Nullable
GHOrganization loadOrganization(@NonNull String organization) {
try {
if (gh != null && isAuthenticated())
if (isAuthenticated())

Check warning on line 508 in src/main/java/org/jenkinsci/plugins/GithubAuthenticationToken.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 508 is not covered by tests
return getGitHub().getOrganization(organization);
} catch (IOException | RuntimeException e) {
LOGGER.log(Level.FINEST, e.getMessage(), e);
Expand All @@ -516,7 +516,7 @@
@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"))) {

Check warning on line 519 in src/main/java/org/jenkinsci/plugins/GithubAuthenticationToken.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 519 is only partially covered, 4 branches are missing
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;
}
}