GitScm extends AbstractScm { @Override @Nonnull public String getId() { return ID; } GitScm(Reachable parent); static String makeCredentialId(String repositoryUrl); @Override Link getLink(); @Override @Nonnull String getId(); @Override @Nonnull String getUri(); @Override String getCredentialId(); @Override Container<ScmOrganization> getOrganizations(); @Override ScmServerEndpointContainer getServers(); @Override HttpResponse validateAndCreate(@JsonBody JSONObject request); static final String ID; }
@Test public void shouldCreateWithRemoteGitRepo() throws IOException, UnirestException { String accessToken = needsGithubAccessToken(); User user = login(); this.jwtToken = getJwtToken(j.jenkins, user.getId(), user.getId()); Map resp = createCredentials(user, ImmutableMap.of("credentials", new ImmutableMap.Builder<String,Object>() .put("password", accessToken) .put("stapler-class", "com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl") .put("scope", "USER") .put("domain","blueocean-git-domain") .put("description", "joe desc") .put("$class", "com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl") .put("username", "joe").build() )); String credentialId = (String) resp.get("id"); Assert.assertNotNull(credentialId); Map r = new RequestBuilder(baseUrl) .status(201) .jwtToken(getJwtToken(j.jenkins, user.getId(), user.getId())) .crumb( crumb ) .post("/organizations/" + getOrgName() + "/pipelines/") .data(ImmutableMap.of("name", "demo", "$class", "io.jenkins.blueocean.blueocean_git_pipeline.GitPipelineCreateRequest", "scmConfig", ImmutableMap.of("uri", HTTPS_GITHUB_NO_JENKINSFILE, "credentialId", credentialId) )).build(Map.class); assertEquals("demo", r.get("name")); }
@Test public void shouldGetForbiddenForBadCredentialIdOnCreate1() throws IOException, UnirestException { User user = login(); this.jwtToken = getJwtToken(j.jenkins, user.getId(), user.getId()); Map resp = createCredentials(user, ImmutableMap.of("credentials", new ImmutableMap.Builder<String,Object>() .put("privateKeySource", ImmutableMap.of( "privateKey", "abcabc1212", "stapler-class", "com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey$DirectEntryPrivateKeySource")) .put("passphrase", "ssh2") .put("scope", "USER") .put("domain","blueocean-git-domain") .put("description", "ssh2 desc") .put("$class", "com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey") .put("username", "ssh2").build() )); String credentialId = (String) resp.get("id"); Assert.assertNotNull(credentialId); post("/organizations/" + getOrgName() + "/pipelines/", ImmutableMap.of("name", "demo", "$class", "io.jenkins.blueocean.blueocean_git_pipeline.GitPipelineCreateRequest", "scmConfig", ImmutableMap.of("uri", "git@github.com:vivek/capability-annotation.git", "credentialId", credentialId) ), 400); }
@Test public void shouldGetForbiddenForBadCredentialIdOnCreate2() throws IOException, UnirestException { User user = login(); this.jwtToken = getJwtToken(j.jenkins, user.getId(), user.getId()); Map resp = createCredentials(user, ImmutableMap.of("credentials", new ImmutableMap.Builder<String,Object>() .put("password", "abcd") .put("stapler-class", "com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl") .put("scope", "USER") .put("domain","blueocean-git-domain") .put("description", "joe desc") .put("$class", "com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl") .put("username", "joe").build() )); String credentialId = (String) resp.get("id"); Assert.assertNotNull(credentialId); post("/organizations/" + getOrgName() + "/pipelines/", ImmutableMap.of("name", "demo", "$class", "io.jenkins.blueocean.blueocean_git_pipeline.GitPipelineCreateRequest", "scmConfig", ImmutableMap.of("uri", "git@github.com:vivek/capability-annotation.git", "credentialId", credentialId) ), 400); }
@Test public void shouldGetBadRequestForBadGitUriOnCreate() throws Exception { User user = login(); this.jwtToken = getJwtToken(j.jenkins, user.getId(), user.getId()); post("/organizations/" + getOrgName() + "/pipelines/", ImmutableMap.of("name", "demo", "$class", "io.jenkins.blueocean.blueocean_git_pipeline.GitPipelineCreateRequest", "scmConfig", ImmutableMap.of("uri", "/sdsd/sdsd/sdsd") ), 400); }
@Test public void shouldFailForBadCredentialIdOnCreate() throws IOException, UnirestException { User user = login(); Map resp = createCredentials(user, ImmutableMap.of("credentials", new ImmutableMap.Builder<String,Object>() .put("privateKeySource", ImmutableMap.of( "privateKey", "abcabc1212", "stapler-class", "com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey$DirectEntryPrivateKeySource")) .put("passphrase", "ssh2") .put("scope", "USER") .put("domain","blueocean-git-domain") .put("description", "ssh2 desc") .put("$class", "com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey") .put("username", "ssh2").build() ) ); String credentialId = (String) resp.get("id"); Assert.assertNotNull(credentialId); resp = new RequestBuilder(baseUrl) .status(400) .jwtToken(getJwtToken(j.jenkins,user.getId(), user.getId())) .crumb( crumb ) .post("/organizations/" + getOrgName() + "/pipelines/") .data(ImmutableMap.of("name", "demo", "$class", "io.jenkins.blueocean.blueocean_git_pipeline.GitPipelineCreateRequest", "scmConfig", ImmutableMap.of("uri", "git@github.com:vivek/capability-annotation.git", "credentialId", credentialId))).build(Map.class); assertEquals(resp.get("code"), 400); List<Map> errors = (List<Map>) resp.get("errors"); assertEquals(1, errors.size()); assertEquals(errors.get(0).toString(), "scmConfig.credentialId", errors.get(0).get("field")); assertEquals(errors.get(0).toString(), "INVALID", errors.get(0).get("code")); }
@Test public void shouldFailOnValidation2() throws Exception { User user = login(); this.jwtToken = getJwtToken(j.jenkins, user.getId(), user.getId()); Map<String,Object> resp = post("/organizations/" + getOrgName() + "/pipelines/", ImmutableMap.of("name", "demo", "$class", "io.jenkins.blueocean.blueocean_git_pipeline.GitPipelineCreateRequest" ), 400); assertEquals(resp.get("code"), 400); List<Map> errors = (List<Map>) resp.get("errors"); assertEquals("scmConfig", errors.get(0).get("field")); assertEquals("MISSING", errors.get(0).get("code")); assertNull(getOrgRoot().getItem("demo")); }
@Test public void shouldFailOnValidation5() throws Exception { User user = login(); this.jwtToken = getJwtToken(j.jenkins, user.getId(), user.getId()); Map<String,Object> resp = post("/organizations/" + getOrgName() + "/pipelines/", ImmutableMap.of("name", "demo", "$class", "io.jenkins.blueocean.blueocean_git_pipeline.GitPipelineCreateRequest", "scmConfig", ImmutableMap.of("uri", sampleRepo.fileUrl(), "credentialId", "sdsdsd") ), 400); List<Map<String,String>> errors = (List<Map<String,String>>) resp.get("errors"); assertEquals("scmConfig.credentialId", errors.get(0).get("field")); assertEquals("NOT_FOUND", errors.get(0).get("code")); assertNull(getOrgRoot().getItem("demo")); }
@Test public void shouldNotProvideIdForMissingCredentials() throws Exception { User user = login(); String scmPath = "/organizations/" + getOrgName() + "/scm/git/"; String repoPath = scmPath + "?repositoryUrl=" + HTTPS_GITHUB_PUBLIC; Map resp = new RequestBuilder(baseUrl) .status(200) .jwtToken(getJwtToken(j.jenkins,user.getId(), user.getId())) .crumb( crumb ) .get(repoPath) .build(Map.class); assertEquals(null, resp.get("credentialId")); }
@Test public void shouldBePoliteAboutBadUrl() throws Exception { User user = login(); String scmPath = "/organizations/" + getOrgName() + "/scm/git/"; String repoPath = scmPath + "?repositoryUrl=htt"; Map resp = new RequestBuilder(baseUrl) .status(200) .jwtToken(getJwtToken(j.jenkins,user.getId(), user.getId())) .crumb( crumb ) .get(repoPath) .build(Map.class); assertEquals(null, resp.get("credentialId")); }
@Test public void shouldCreateCredentialsWithDefaultId() throws Exception { User user = login(); String scmPath = "/organizations/" + getOrgName() + "/scm/git/"; String scmValidatePath = scmPath + "validate"; Map params = ImmutableMap.of( "userName", "someguy", "password", "password", "repositoryUrl", HTTPS_GITHUB_PUBLIC ); Map resp = new RequestBuilder(baseUrl) .status(200) .jwtToken(getJwtToken(j.jenkins,user.getId(), user.getId())) .crumb( crumb ) .data(params) .put(scmValidatePath) .build(Map.class); assertEquals("ok", resp.get("status")); String repoPath = scmPath + "?repositoryUrl=" + HTTPS_GITHUB_PUBLIC; Map resp2 = new RequestBuilder(baseUrl) .status(200) .jwtToken(getJwtToken(j.jenkins,user.getId(), user.getId())) .crumb( crumb ) .get(repoPath) .build(Map.class); assertEquals("git:" + HTTPS_GITHUB_PUBLIC_HASH, resp2.get("credentialId")); }
@Test public void shouldNotCreateCredentialsForBadUrl1() throws Exception { User user = login(); String scmPath = "/organizations/" + getOrgName() + "/scm/git/"; String scmValidatePath = scmPath + "validate"; Map params = ImmutableMap.of( "userName", "someguy", "password", "password", "repositoryUrl", "htt" ); Map resp = new RequestBuilder(baseUrl) .status(400) .jwtToken(getJwtToken(j.jenkins,user.getId(), user.getId())) .crumb( crumb ) .data(params) .put(scmValidatePath) .build(Map.class); assertTrue(resp.get("message").toString().toLowerCase().contains("invalid url")); }
@Test public void shouldNotCreateCredentialsForBadUrl2() throws Exception { User user = login(); String scmPath = "/organizations/" + getOrgName() + "/scm/git/"; String scmValidatePath = scmPath + "validate"; Map params = ImmutableMap.of( "userName", "someguy", "password", "password", "repositoryUrl", "http: ); Map resp = new RequestBuilder(baseUrl) .status(428) .jwtToken(getJwtToken(j.jenkins,user.getId(), user.getId())) .crumb( crumb ) .data(params) .put(scmValidatePath) .build(Map.class); assertTrue(resp.get("message").toString().toLowerCase().contains("url unreachable")); }