Search in sources :

Example 91 with User

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");
}
Also used : User(hudson.model.User) StaplerRequest(org.kohsuke.stapler.StaplerRequest) Mailer(hudson.tasks.Mailer) GitContent(io.jenkins.blueocean.rest.impl.pipeline.scm.GitContent) JSONObject(net.sf.json.JSONObject) ServiceException(io.jenkins.blueocean.commons.ServiceException) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) BitbucketScmSaveFileRequest(io.jenkins.blueocean.blueocean_bitbucket_pipeline.BitbucketScmSaveFileRequest) MultiBranchProject(jenkins.branch.MultiBranchProject) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 92 with User

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");
}
Also used : User(hudson.model.User) IdCredentials(com.cloudbees.plugins.credentials.common.IdCredentials) CreateResponse(io.jenkins.blueocean.rest.model.CreateResponse) JSONObject(net.sf.json.JSONObject) CredentialsStoreAction(com.cloudbees.plugins.credentials.CredentialsStoreAction) WebMethod(org.kohsuke.stapler.WebMethod) POST(org.kohsuke.stapler.verb.POST)

Example 93 with User

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"));
}
Also used : User(hudson.model.User) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.junit.Test)

Example 94 with User

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;
}
Also used : UserDetails(org.acegisecurity.userdetails.UserDetails) Mailer(hudson.tasks.Mailer) User(hudson.model.User) PrincipalAcegiUserToken(org.acegisecurity.adapters.PrincipalAcegiUserToken)

Example 95 with User

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();
}
Also used : User(hudson.model.User)

Aggregations

User (hudson.model.User)105 Test (org.junit.Test)71 Map (java.util.Map)42 ImmutableMap (com.google.common.collect.ImmutableMap)38 PipelineBaseTest (io.jenkins.blueocean.rest.impl.pipeline.PipelineBaseTest)26 Mailer (hudson.tasks.Mailer)24 ServiceException (io.jenkins.blueocean.commons.ServiceException)21 StaplerRequest (org.kohsuke.stapler.StaplerRequest)14 MultiBranchProject (jenkins.branch.MultiBranchProject)13 List (java.util.List)12 JSONObject (net.sf.json.JSONObject)12 GitContent (io.jenkins.blueocean.rest.impl.pipeline.scm.GitContent)8 IOException (java.io.IOException)8 Domain (com.cloudbees.plugins.credentials.domains.Domain)7 BlueOceanDomainRequirement (io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainRequirement)7 Authentication (org.acegisecurity.Authentication)7 CredentialsStore (com.cloudbees.plugins.credentials.CredentialsStore)6 StandardUsernamePasswordCredentials (com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials)6 BufferedReader (java.io.BufferedReader)6 StringReader (java.io.StringReader)6