Search in sources :

Example 1 with Context

use of com.google.gerrit.acceptance.AcceptanceTestRequestScope.Context in project gerrit by GerritCodeReview.

the class AbstractDaemonTest method beforeTest.

protected void beforeTest(Description description) throws Exception {
    // SystemReader must be overridden before creating any repos, since they read the user/system
    // configs at initialization time, and are then stored in the RepositoryCache forever.
    oldSystemReader = setFakeSystemReader(temporaryFolder.getRoot());
    this.description = description;
    GerritServer.Description classDesc = GerritServer.Description.forTestClass(description, configName);
    GerritServer.Description methodDesc = GerritServer.Description.forTestMethod(description, configName);
    testMethodDescription = methodDesc;
    testRequiresSsh = classDesc.useSshAnnotation() || methodDesc.useSshAnnotation();
    if (!testRequiresSsh) {
        baseConfig.setString("sshd", null, "listenAddress", "off");
    }
    baseConfig.unset("gerrit", null, "canonicalWebUrl");
    baseConfig.unset("httpd", null, "listenUrl");
    baseConfig.setInt("index", null, "batchThreads", -1);
    baseConfig.setInt("receive", null, "changeUpdateThreads", 4);
    Module module = createModule();
    Module auditModule = createAuditModule();
    Module sshModule = createSshModule();
    if (classDesc.equals(methodDesc) && !classDesc.sandboxed() && !methodDesc.sandboxed()) {
        if (commonServer == null) {
            commonServer = GerritServer.initAndStart(temporaryFolder, classDesc, baseConfig, module, auditModule, sshModule);
        }
        server = commonServer;
    } else {
        server = GerritServer.initAndStart(temporaryFolder, methodDesc, baseConfig, module, auditModule, sshModule);
    }
    server.getTestInjector().injectMembers(this);
    Transport.register(inProcessProtocol);
    toClose = Collections.synchronizedList(new ArrayList<>());
    admin = accountCreator.admin();
    user = accountCreator.user1();
    // Evict and reindex accounts in case tests modify them.
    reindexAccount(admin.id());
    reindexAccount(user.id());
    adminRestSession = new RestSession(server, admin);
    userRestSession = new RestSession(server, user);
    anonymousRestSession = new RestSession(server, null);
    initSsh();
    resourcePrefix = UNSAFE_PROJECT_NAME.matcher(description.getClassName() + "_" + description.getMethodName() + "_").replaceAll("");
    Context ctx = newRequestContext(admin);
    atrScope.set(ctx);
    ProjectInput in = projectInput(description);
    gApi.projects().create(in);
    project = Project.nameKey(in.name);
    if (!classDesc.skipProjectClone()) {
        testRepo = cloneProject(project, getCloneAsAccount(description));
    }
    // Set the clock step last, so that the test setup isn't consuming any timestamps after the
    // clock has been set.
    setTimeSettings(classDesc.useSystemTime(), classDesc.useClockStep(), classDesc.useTimezone());
    setTimeSettings(methodDesc.useSystemTime(), methodDesc.useClockStep(), methodDesc.useTimezone());
}
Also used : Context(com.google.gerrit.acceptance.AcceptanceTestRequestScope.Context) ProjectInput(com.google.gerrit.extensions.api.projects.ProjectInput) ArrayList(java.util.ArrayList) Module(com.google.inject.Module)

Example 2 with Context

use of com.google.gerrit.acceptance.AcceptanceTestRequestScope.Context in project gerrit by GerritCodeReview.

the class AbstractDaemonTest method initSsh.

protected void initSsh() throws Exception {
    if (testRequiresSsh && SshMode.useSsh() && (adminSshSession == null || userSshSession == null)) {
        // Create Ssh sessions
        SshSessionFactory.initSsh();
        Context ctx = newRequestContext(user);
        atrScope.set(ctx);
        userSshSession = ctx.getSession();
        userSshSession.open();
        ctx = newRequestContext(admin);
        atrScope.set(ctx);
        adminSshSession = ctx.getSession();
        adminSshSession.open();
    }
}
Also used : Context(com.google.gerrit.acceptance.AcceptanceTestRequestScope.Context)

Example 3 with Context

use of com.google.gerrit.acceptance.AcceptanceTestRequestScope.Context in project gerrit by GerritCodeReview.

the class EmailIT method setPreferredEmailToEmailFromCustomRealmThatDoesntExistAsExternalId.

@Test
public void setPreferredEmailToEmailFromCustomRealmThatDoesntExistAsExternalId() throws Exception {
    String email = "foo@example.com";
    ExternalId.Key mailtoExtIdKey = externalIdKeyFactory.create(ExternalId.SCHEME_MAILTO, email);
    assertThat(externalIds.get(mailtoExtIdKey)).isEmpty();
    assertThat(gApi.accounts().self().get().email).isNotEqualTo(email);
    Context oldCtx = createContextWithCustomRealm(new RealmWithAdditionalEmails(admin.id(), email));
    try {
        gApi.accounts().self().email(email).setPreferred();
        Optional<ExternalId> mailtoExtId = externalIds.get(mailtoExtIdKey);
        assertThat(mailtoExtId).isPresent();
        assertThat(mailtoExtId.get().accountId()).isEqualTo(admin.id());
        assertThat(gApi.accounts().self().get().email).isEqualTo(email);
    } finally {
        atrScope.set(oldCtx);
    }
}
Also used : Context(com.google.gerrit.acceptance.AcceptanceTestRequestScope.Context) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) IdString(com.google.gerrit.extensions.restapi.IdString) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 4 with Context

use of com.google.gerrit.acceptance.AcceptanceTestRequestScope.Context in project gerrit by GerritCodeReview.

the class AbstractDaemonTest method disableNoteDb.

protected AutoCloseable disableNoteDb() {
    changeNotesArgs.failOnLoadForTest.set(true);
    Context oldContext = atrScope.disableNoteDb();
    return () -> {
        changeNotesArgs.failOnLoadForTest.set(false);
        atrScope.set(oldContext);
    };
}
Also used : Context(com.google.gerrit.acceptance.AcceptanceTestRequestScope.Context)

Example 5 with Context

use of com.google.gerrit.acceptance.AcceptanceTestRequestScope.Context in project gerrit by GerritCodeReview.

the class EmailIT method setPreferredEmailToEmailFromCustomRealmThatBelongsToOtherAccount.

@Test
public void setPreferredEmailToEmailFromCustomRealmThatBelongsToOtherAccount() throws Exception {
    ExternalId mailToExtId = externalIdFactory.createEmail(user.id(), user.email());
    assertThat(externalIds.get(mailToExtId.key())).isPresent();
    Context oldCtx = createContextWithCustomRealm(new RealmWithAdditionalEmails(admin.id(), user.email()));
    try {
        ResourceConflictException thrown = assertThrows(ResourceConflictException.class, () -> gApi.accounts().self().email(user.email()).setPreferred());
        assertThat(thrown).hasMessageThat().contains("email in use by another account");
    } finally {
        atrScope.set(oldCtx);
    }
}
Also used : Context(com.google.gerrit.acceptance.AcceptanceTestRequestScope.Context) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Aggregations

Context (com.google.gerrit.acceptance.AcceptanceTestRequestScope.Context)5 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)2 ExternalId (com.google.gerrit.server.account.externalids.ExternalId)2 Test (org.junit.Test)2 ProjectInput (com.google.gerrit.extensions.api.projects.ProjectInput)1 IdString (com.google.gerrit.extensions.restapi.IdString)1 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)1 Module (com.google.inject.Module)1 ArrayList (java.util.ArrayList)1