use of hudson.model.User in project blueocean-plugin by jenkinsci.
the class BitbucketServerScmContentProviderTest method unauthorizedSaveContentShouldFail.
@Test
public void unauthorizedSaveContentShouldFail() throws UnirestException, IOException {
User alice = User.get("alice");
alice.setFullName("Alice Cooper");
alice.addProperty(new Mailer.UserProperty("alice@jenkins-ci.org"));
String aliceCredentialId = createCredential(BitbucketServerScm.ID, alice);
StaplerRequest staplerRequest = mockStapler();
MultiBranchProject mbp = mockMbp(aliceCredentialId, alice);
GitContent content = new GitContent.Builder().autoCreateBranch(true).base64Data("bm9kZXsKICBlY2hvICdoZWxsbyB3b3JsZCEnCn0K").branch("master").message("new commit").owner("TESTP").path("README.md").repo("pipeline-demo-test").build();
when(staplerRequest.bindJSON(Mockito.eq(BitbucketScmSaveFileRequest.class), Mockito.any(JSONObject.class))).thenReturn(new BitbucketScmSaveFileRequest(content));
String request = "{\n" + " \"content\" : {\n" + " \"message\" : \"new commit\",\n" + " \"path\" : \"README.md\",\n" + " \"branch\" : \"master\",\n" + " \"repo\" : \"pipeline-demo-test\",\n" + " \"base64Data\" : " + "\"bm9kZXsKICBlY2hvICdoZWxsbyB3b3JsZCEnCn0K\"" + " }\n" + "}";
when(staplerRequest.getReader()).thenReturn(new BufferedReader(new StringReader(request), request.length()));
try {
new BitbucketServerScmContentProvider().saveContent(staplerRequest, mbp);
} catch (ServiceException.PreconditionRequired e) {
assertEquals("Can't access content from Bitbucket: no credential found", e.getMessage());
return;
}
fail("Should have failed with PreConditionException");
}
use of hudson.model.User in project blueocean-plugin by jenkinsci.
the class CredentialApi method create.
@POST
@WebMethod(name = "")
public CreateResponse create(@JsonBody JSONObject body, StaplerRequest request) throws IOException {
User authenticatedUser = User.current();
if (authenticatedUser == null) {
throw new ServiceException.UnauthorizedException("No authenticated user found");
}
JSONObject jsonObject = body.getJSONObject("credentials");
final IdCredentials credentials = request.bindJSON(IdCredentials.class, jsonObject);
String domainName = DOMAIN_NAME;
if (jsonObject.get("domain") != null && jsonObject.get("domain") instanceof String) {
domainName = (String) jsonObject.get("domain");
}
CredentialsUtils.createCredentialsInUserStore(credentials, authenticatedUser, domainName, ImmutableList.<DomainSpecification>of(new BlueOceanDomainSpecification()));
CredentialsStoreAction.DomainWrapper domainWrapper = credentialStoreAction.getDomain(domainName);
if (domainWrapper != null) {
CredentialsStoreAction.CredentialsWrapper credentialsWrapper = domainWrapper.getCredential(credentials.getId());
if (credentialsWrapper != null) {
return new CreateResponse(new CredentialApi.Credential(credentialsWrapper, getLink().rel("domains").rel(domainName).rel("credentials")));
}
}
// this should never happen
throw new ServiceException.UnexpectedErrorException("Unexpected error, failed to create credential");
}
use of hudson.model.User in project blueocean-plugin by jenkinsci.
the class CredentialApiTest method createSshCredentialUsingDirectSshInUserStore.
@Test
public void createSshCredentialUsingDirectSshInUserStore() throws IOException, UnirestException {
User user = login();
Map resp = new RequestBuilder(baseUrl).status(201).jwtToken(getJwtToken(j.jenkins, user.getId(), user.getId())).post("/organizations/jenkins/credentials/user/").data(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())).build(Map.class);
Assert.assertEquals("SSH Username with private key", resp.get("typeName"));
Assert.assertEquals("blueocean-git-domain", resp.get("domain"));
String credId = (String) resp.get("id");
assertNotNull(credId);
String credUri = getUrlFromHref(getHrefFromLinks(resp, "self"));
resp = new RequestBuilder(baseUrl).status(200).jwtToken(getJwtToken(j.jenkins, user.getId(), user.getId())).get(credUri).build(Map.class);
Assert.assertEquals("SSH Username with private key", resp.get("typeName"));
Assert.assertEquals("blueocean-git-domain", resp.get("domain"));
}
use of hudson.model.User in project blueocean-plugin by jenkinsci.
the class PipelineBaseTest method login.
protected User login(String userId, String fullName, String email) throws IOException {
j.jenkins.setSecurityRealm(j.createDummySecurityRealm());
hudson.model.User bob = User.get(userId);
bob.setFullName(fullName);
if (email != null) {
bob.addProperty(new Mailer.UserProperty(email));
}
UserDetails d = Jenkins.getInstance().getSecurityRealm().loadUserByUsername(bob.getId());
SecurityContextHolder.getContext().setAuthentication(new PrincipalAcegiUserToken(bob.getId(), bob.getId(), bob.getId(), d.getAuthorities(), bob.getId()));
return bob;
}
use of hudson.model.User in project blueocean-plugin by jenkinsci.
the class AbstractAnalytics method identity.
protected final String identity(String server) {
User user = User.current();
String username = user == null ? "ANONYMOUS" : user.getId();
return Hashing.sha256().hashString(username + server).toString();
}
Aggregations