Search in sources :

Example 6 with MockRequestAssertion

use of com.google.copybara.testing.git.GitTestUtil.MockRequestAssertion in project copybara by google.

the class GerritDestinationTest method gerritSubmit_plusTwoRestricted.

@Test
public void gerritSubmit_plusTwoRestricted() throws Exception {
    options.gerrit.gerritChangeId = null;
    fetch = "master";
    writeFile(workdir, "file", "some content");
    url = BASE_URL + "/foo/bar";
    repoGitDir = gitUtil.mockRemoteRepo("user:SECRET@copybara-not-real.com/foo/bar").getGitDir();
    gitUtil.mockApi(eq("GET"), startsWith(BASE_URL + "/changes/"), mockResponse("[" + "{" + "  change_id : \"Iaaaaaaaaaabbbbbbbbbbccccccccccdddddddddd\"," + "  status : \"NEW\"" + "}]"));
    AtomicBoolean submitCalled = new AtomicBoolean(false);
    AtomicBoolean reviewCalled = new AtomicBoolean(false);
    gitUtil.mockApi(eq("POST"), matches(BASE_URL + "/changes/.*/revisions/.*/review"), mockResponseWithStatus("Applying label \\\"Code-Review\\\": 2 is restricted\\n\\n", 401));
    gitUtil.mockApi(eq("POST"), matches(BASE_URL + "/changes/.*/submit"), mockResponseAndValidateRequest("{" + "  change_id : \"Iaaaaaaaaaabbbbbbbbbbccccccccccdddddddddd\"," + "  status : \"submitted\"" + "}", new MockRequestAssertion("Always true with side-effect", s -> {
        submitCalled.set(true);
        return true;
    })));
    options.setForce(true);
    DummyRevision originRef = new DummyRevision("origin_ref");
    GerritDestination destination = destination("submit = True", "gerrit_submit = True");
    Glob glob = Glob.createGlob(ImmutableList.of("**"), excludedDestinationPaths);
    WriterContext writerContext = new WriterContext("GerritDestinationTest", "test", false, originRef, Glob.ALL_FILES.roots());
    ValidationException validationException = assertThrows(ValidationException.class, () -> destination.newWriter(writerContext).write(TransformResults.of(workdir, originRef).withSummary("Test message").withIdentity(originRef.asString()), glob, console));
    assertThat(validationException).hasMessageThat().contains("2 is restricted");
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WriterContext(com.google.copybara.WriterContext) MockRequestAssertion(com.google.copybara.testing.git.GitTestUtil.MockRequestAssertion) ValidationException(com.google.copybara.exception.ValidationException) DummyRevision(com.google.copybara.testing.DummyRevision) Glob(com.google.copybara.util.Glob) Test(org.junit.Test)

Example 7 with MockRequestAssertion

use of com.google.copybara.testing.git.GitTestUtil.MockRequestAssertion in project copybara by google.

the class GitHubEndpointTest method testFeedbackDeleteReference.

@Test
public void testFeedbackDeleteReference() throws Exception {
    AtomicBoolean called = new AtomicBoolean(false);
    gitUtil.mockApi(eq("DELETE"), contains("/git/refs/heads/test"), mockResponseWithStatus("", 202, new MockRequestAssertion("Always true with side-effect", s -> {
        called.set(true);
        return true;
    })));
    runFeedback(ImmutableList.of("ctx.destination.delete_reference('refs/heads/test')"));
    assertThat(called.get()).isTrue();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MockRequestAssertion(com.google.copybara.testing.git.GitTestUtil.MockRequestAssertion) Test(org.junit.Test)

Example 8 with MockRequestAssertion

use of com.google.copybara.testing.git.GitTestUtil.MockRequestAssertion in project copybara by google.

the class GitHubEndpointTest method testFeedbackDeleteReference_masterCheck.

@Test
public void testFeedbackDeleteReference_masterCheck() {
    AtomicBoolean called = new AtomicBoolean(false);
    gitUtil.mockApi(eq("DELETE"), contains("/git/refs/heads/master"), mockResponseWithStatus("", 202, new MockRequestAssertion("Always true with side-effect", s -> {
        called.set(true);
        return true;
    })));
    ValidationException expected = assertThrows(ValidationException.class, () -> runFeedback(ImmutableList.of("ctx.destination.delete_reference('refs/heads/master')")));
    assertThat(expected).hasMessageThat().contains("Copybara doesn't allow to delete master");
    assertThat(called.get()).isFalse();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MockRequestAssertion(com.google.copybara.testing.git.GitTestUtil.MockRequestAssertion) ValidationException(com.google.copybara.exception.ValidationException) Test(org.junit.Test)

Aggregations

MockRequestAssertion (com.google.copybara.testing.git.GitTestUtil.MockRequestAssertion)8 Test (org.junit.Test)8 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 WriterContext (com.google.copybara.WriterContext)4 DummyRevision (com.google.copybara.testing.DummyRevision)4 Glob (com.google.copybara.util.Glob)4 DestinationEffect (com.google.copybara.DestinationEffect)3 ValidationException (com.google.copybara.exception.ValidationException)2 Feedback (com.google.copybara.feedback.Feedback)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1