Search in sources :

Example 1 with User

use of hudson.model.User in project hudson-2.x by hudson.

the class FunctionsTest method testIsAuthorUserIdNull.

@Test
@PrepareForTest(User.class)
public void testIsAuthorUserIdNull() throws Exception {
    mockStatic(User.class);
    User user = createMock(User.class);
    expect(user.getId()).andReturn(null);
    expect(User.current()).andReturn(user);
    Job job = createMock(FreeStyleProject.class);
    expect(job.getCreatedBy()).andReturn(USER).times(2);
    replay(User.class, user, job);
    boolean result = Functions.isAuthor(job);
    verify(User.class, user, job);
    assertFalse(result);
}
Also used : User(hudson.model.User) Job(hudson.model.Job) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 2 with User

use of hudson.model.User in project hudson-2.x by hudson.

the class FunctionsTest method testIsAuthorTrue.

@Test
@PrepareForTest(User.class)
public void testIsAuthorTrue() throws Exception {
    mockStatic(User.class);
    User user = createMock(User.class);
    expect(user.getId()).andReturn(USER);
    expect(User.current()).andReturn(user);
    Job job = createMock(FreeStyleProject.class);
    expect(job.getCreatedBy()).andReturn(USER).times(2);
    replay(User.class, user, job);
    boolean result = Functions.isAuthor(job);
    verify(User.class, user, job);
    assertTrue(result);
}
Also used : User(hudson.model.User) Job(hudson.model.Job) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 3 with User

use of hudson.model.User in project hudson-2.x by hudson.

the class FunctionsTest method testIsAuthorFalse.

@Test
@PrepareForTest(User.class)
public void testIsAuthorFalse() throws Exception {
    mockStatic(User.class);
    User user = createMock(User.class);
    expect(user.getId()).andReturn(USER);
    expect(User.current()).andReturn(user);
    Job job = createMock(FreeStyleProject.class);
    expect(job.getCreatedBy()).andReturn("user").times(2);
    replay(User.class, user, job);
    boolean result = Functions.isAuthor(job);
    verify(User.class, user, job);
    assertFalse(result);
}
Also used : User(hudson.model.User) Job(hudson.model.Job) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 4 with User

use of hudson.model.User in project blueocean-plugin by jenkinsci.

the class BlueOceanCredentialsProvider method getCredentials.

@Nonnull
public <C extends Credentials> List<C> getCredentials(@Nonnull final Class<C> type, @Nullable ItemGroup itemGroup, @Nullable Authentication authentication, @Nonnull List<DomainRequirement> domainRequirements) {
    final List<C> result = new ArrayList<>();
    final FolderPropertyImpl prop = propertyOf(itemGroup);
    if (prop != null && prop.domain.test(domainRequirements)) {
        final User proxyUser = User.get(prop.getUser(), false, Collections.emptyMap());
        Authentication proxyAuth = proxyUser == null ? null : proxyUser.impersonate();
        if (proxyAuth != null) {
            ACL.impersonate(proxyAuth, new Runnable() {

                @Override
                public void run() {
                    for (CredentialsStore s : CredentialsProvider.lookupStores(proxyUser)) {
                        for (Domain d : s.getDomains()) {
                            if (d.test(PROXY_REQUIREMENT)) {
                                for (Credentials c : filter(s.getCredentials(d), withId(prop.getId()))) {
                                    if (type.isInstance(c)) {
                                        result.add((C) c);
                                    }
                                }
                            }
                        }
                    }
                }
            });
        }
    }
    return result;
}
Also used : User(hudson.model.User) Authentication(org.acegisecurity.Authentication) ArrayList(java.util.ArrayList) CredentialsStore(com.cloudbees.plugins.credentials.CredentialsStore) Domain(com.cloudbees.plugins.credentials.domains.Domain) Credentials(com.cloudbees.plugins.credentials.Credentials) IdCredentials(com.cloudbees.plugins.credentials.common.IdCredentials) Nonnull(javax.annotation.Nonnull)

Example 5 with User

use of hudson.model.User in project blueocean-plugin by jenkinsci.

the class CredentialContainer method iterator.

@Override
public Iterator<CredentialApi> iterator() {
    List<CredentialApi> apis = new ArrayList<>();
    User user = User.current();
    if (user != null) {
        for (CredentialsStore store : CredentialsProvider.lookupStores(user)) {
            if (store.getStoreAction() != null) {
                apis.add(new CredentialApi(store.getStoreAction(), this));
            }
        }
    } else {
        for (CredentialsStore store : CredentialsProvider.lookupStores(Jenkins.getInstance())) {
            if (store.getStoreAction() != null) {
                apis.add(new CredentialApi(store.getStoreAction(), this));
            }
        }
    }
    return apis.iterator();
}
Also used : User(hudson.model.User) ArrayList(java.util.ArrayList) CredentialsStore(com.cloudbees.plugins.credentials.CredentialsStore)

Aggregations

User (hudson.model.User)48 Test (org.junit.Test)30 Map (java.util.Map)22 ImmutableMap (com.google.common.collect.ImmutableMap)21 PipelineBaseTest (io.jenkins.blueocean.rest.impl.pipeline.PipelineBaseTest)11 Mailer (hudson.tasks.Mailer)8 Domain (com.cloudbees.plugins.credentials.domains.Domain)7 CredentialsStore (com.cloudbees.plugins.credentials.CredentialsStore)5 BlueOceanDomainRequirement (io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainRequirement)5 FreeStyleProject (hudson.model.FreeStyleProject)4 Job (hudson.model.Job)4 ServiceException (io.jenkins.blueocean.commons.ServiceException)4 List (java.util.List)4 StandardUsernamePasswordCredentials (com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials)3 Cause (hudson.model.Cause)3 IOException (java.io.IOException)3 OrganizationFolder (jenkins.branch.OrganizationFolder)3 Authentication (org.acegisecurity.Authentication)3 MockFolder (org.jvnet.hudson.test.MockFolder)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3