Skip to content

Commit

Permalink
[JENKINS-42955] Unit test for issue
Browse files Browse the repository at this point in the history
  • Loading branch information
raul-arabaolaza committed Apr 2, 2017
1 parent 6d71a66 commit 5409f11
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Expand Up @@ -99,6 +99,12 @@
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>cloudbees-folder</artifactId>
<version>6.0.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
Expand Down
@@ -1,8 +1,13 @@
package org.jenkinsci.plugins.pipeline.githubstatusnotification;

import com.cloudbees.hudson.plugins.folder.Folder;
import com.cloudbees.hudson.plugins.folder.properties.FolderCredentialsProvider;
import com.cloudbees.plugins.credentials.Credentials;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.CredentialsScope;
import com.cloudbees.plugins.credentials.CredentialsStore;
import com.cloudbees.plugins.credentials.SystemCredentialsProvider;
import com.cloudbees.plugins.credentials.domains.Domain;
import hudson.model.Result;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
Expand Down Expand Up @@ -290,6 +295,36 @@ public void build() throws Exception {
jenkins.assertBuildStatus(Result.SUCCESS, jenkins.waitForCompletion(b1));
}

@Test
public void buildWitFolderCredentials() throws Exception {

GitHub gh = PowerMockito.mock(GitHub.class);
PowerMockito.mockStatic(GitHub.class);
PowerMockito.when(GitHub.connect("user", "password")).thenReturn(gh);
PowerMockito.when(gh.isCredentialValid()).thenReturn(true);
GHRepository repo = PowerMockito.mock(GHRepository.class);
GHUser user = PowerMockito.mock(GHUser.class);
GHCommit commit = PowerMockito.mock(GHCommit.class);
PowerMockito.when(user.getRepository(anyString())).thenReturn(repo);
PowerMockito.when(gh.getUser(anyString())).thenReturn(user);
PowerMockito.when((repo.getCommit(anyString()))).thenReturn(commit);

Folder f = jenkins.jenkins.createProject(Folder.class, "folder" + jenkins.jenkins.getItems().size());
CredentialsStore folderStore = getFolderStore(f);
folderStore.addCredentials(Domain.global(),
new DummyCredentials(CredentialsScope.GLOBAL, "user", "password"));

WorkflowJob p = f.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition(
"githubNotify account: 'raul-arabaolaza', context: 'PCT Results', " +
"credentialsId: 'dummy', description: 'PCT Is OK', " +
"repo: 'acceptance-test-harness', sha: '0b5936eb903d439ac0c0bf84940d73128d5e9487', " +
"status: 'SUCCESS', targetUrl: 'http://www.cloudbees.com'"
));
WorkflowRun b1 = p.scheduleBuild2(0).waitForStart();
jenkins.assertBuildStatus(Result.SUCCESS, jenkins.waitForCompletion(b1));
}

@Test
public void buildEnterprise() throws Exception {

Expand Down Expand Up @@ -318,4 +353,17 @@ public void buildEnterprise() throws Exception {
jenkins.assertBuildStatus(Result.SUCCESS, jenkins.waitForCompletion(b1));
}

private CredentialsStore getFolderStore(Folder f) {
Iterable<CredentialsStore> stores = CredentialsProvider.lookupStores(f);
CredentialsStore folderStore = null;
for (CredentialsStore s : stores) {
if (s.getProvider() instanceof FolderCredentialsProvider && s.getContext() == f) {
folderStore = s;
break;
}
}
return folderStore;
}


}

0 comments on commit 5409f11

Please sign in to comment.