use of com.cloudbees.plugins.credentials.domains.Domain in project blueocean-plugin by jenkinsci.
the class CredentialsUtils method findOrCreateDomain.
@Nonnull
private static Domain findOrCreateDomain(@Nonnull CredentialsStore store, @Nonnull String domainName, @Nonnull List<DomainSpecification> domainSpecifications) throws IOException {
Domain domain = store.getDomainByName(domainName);
if (domain == null) {
//create new one
boolean result = store.addDomain(new Domain(domainName, domainName + " to store credentials by BlueOcean", domainSpecifications));
if (!result) {
throw new ServiceException.BadRequestExpception("Failed to create credential domain: " + domainName);
}
domain = store.getDomainByName(domainName);
if (domain == null) {
throw new ServiceException.UnexpectedErrorException("Domain %s created but not found");
}
}
return domain;
}
use of com.cloudbees.plugins.credentials.domains.Domain in project nodejs-plugin by jenkinsci.
the class NPMRegistryValidatorTest method test_credentials_ok.
@Test
public void test_credentials_ok() throws Exception {
String credentialsId = "secret";
Credentials credentials = new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, credentialsId, "", "user", "password");
Map<Domain, List<Credentials>> credentialsMap = new HashMap<>();
credentialsMap.put(Domain.global(), Arrays.asList(credentials));
SystemCredentialsProvider.getInstance().setDomainCredentialsMap(credentialsMap);
FreeStyleProject prj = mock(FreeStyleProject.class);
when(prj.hasPermission(isA(Permission.class))).thenReturn(true);
DescriptorImpl descriptor = mock(DescriptorImpl.class);
when(descriptor.doCheckCredentialsId(any(Item.class), (String) any(), anyString())).thenCallRealMethod();
String serverURL = "http://acme.com";
FormValidation result = descriptor.doCheckCredentialsId(prj, credentialsId, serverURL);
assertThat(result.kind, is(Kind.OK));
}
use of com.cloudbees.plugins.credentials.domains.Domain in project blueocean-plugin by jenkinsci.
the class GithubMockBase method mockMbp.
protected MultiBranchProject mockMbp(String credentialId, User user, String credentialDomainName) {
MultiBranchProject mbp = mock(MultiBranchProject.class);
when(mbp.getName()).thenReturn("PR-demo");
when(mbp.getParent()).thenReturn(j.jenkins);
GitHubSCMSource scmSource = mock(GitHubSCMSource.class);
when(scmSource.getApiUri()).thenReturn(githubApiUrl);
when(scmSource.getCredentialsId()).thenReturn(credentialId);
when(scmSource.getRepoOwner()).thenReturn("cloudbeers");
when(scmSource.getRepository()).thenReturn("PR-demo");
when(mbp.getSCMSources()).thenReturn(Collections.singletonList(scmSource));
BlueOceanCredentialsProvider.FolderPropertyImpl folderProperty = mock(BlueOceanCredentialsProvider.FolderPropertyImpl.class);
DescribableList<AbstractFolderProperty<?>, AbstractFolderPropertyDescriptor> mbpProperties = new DescribableList<>(mbp);
mbpProperties.add(new BlueOceanCredentialsProvider.FolderPropertyImpl(user.getId(), credentialId, BlueOceanCredentialsProvider.createDomain(githubApiUrl)));
Domain domain = mock(Domain.class);
when(domain.getName()).thenReturn(credentialDomainName);
when(folderProperty.getDomain()).thenReturn(domain);
when(mbp.getProperties()).thenReturn(mbpProperties);
return mbp;
}
use of com.cloudbees.plugins.credentials.domains.Domain in project blueocean-plugin by jenkinsci.
the class GithubEnterpriseApiTest method validateGithubToken.
@Test
public void validateGithubToken() throws IOException, UnirestException {
String credentialId = createGithubEnterpriseCredential();
assertEquals(GithubCredentialUtils.computeCredentialId(null, GithubEnterpriseScm.ID, githubApiUrl), credentialId);
// check if this credentialId is created in correct user domain
Domain domain = CredentialsUtils.findDomain(credentialId, user);
assertEquals("blueocean-github-enterprise-domain", domain.getName());
}
use of com.cloudbees.plugins.credentials.domains.Domain in project blueocean-plugin by jenkinsci.
the class GithubApiTest method validateGithubToken.
@Test
public void validateGithubToken() throws IOException, UnirestException {
// check credentialId of this SCM, should be null
createGithubCredential(user);
// check if this credentialId is created in correct user domain
Domain domain = CredentialsUtils.findDomain("github", user);
assertEquals("blueocean-github-domain", domain.getName());
// now that there is github credentials setup, calling scm api to get credential should simply return that.
Map r = new RequestBuilder(baseUrl).status(200).jwtToken(getJwtToken(j.jenkins, user.getId(), user.getId())).get("/organizations/jenkins/scm/github/?apiUrl=" + githubApiUrl).build(Map.class);
assertEquals("github", r.get("credentialId"));
assertEquals("github", r.get("id"));
// now try validating again, it should return the same credentialId
r = new RequestBuilder(baseUrl).data(MapsHelper.of("accessToken", accessToken)).status(200).jwtToken(getJwtToken(j.jenkins, user.getId(), user.getId())).put("/organizations/jenkins/scm/github/validate/?apiUrl=" + githubApiUrl).build(Map.class);
assertEquals("github", r.get("credentialId"));
}
Aggregations