|
| 1 | +// |
| 2 | +// Copyright (C) 2019 The Qt Company |
| 3 | +// |
| 4 | + |
| 5 | +package com.googlesource.gerrit.plugins.qtcodereview; |
| 6 | + |
| 7 | +import static com.google.common.truth.Truth.assertThat; |
| 8 | +import static com.google.gerrit.server.group.SystemGroupBackend.REGISTERED_USERS; |
| 9 | + |
| 10 | +import com.google.gerrit.acceptance.PushOneCommit; |
| 11 | +import com.google.gerrit.acceptance.RestResponse; |
| 12 | +import com.google.gerrit.acceptance.TestPlugin; |
| 13 | +import com.google.gerrit.acceptance.UseSsh; |
| 14 | +import com.google.gerrit.common.data.Permission; |
| 15 | +import com.google.gerrit.mail.Address; |
| 16 | +import com.google.gerrit.mail.EmailHeader; |
| 17 | +import com.google.gerrit.reviewdb.client.Change; |
| 18 | +import com.google.gerrit.server.project.ProjectConfig; |
| 19 | +import com.google.gerrit.server.project.testing.Util; |
| 20 | +import com.google.gerrit.testing.FakeEmailSender; |
| 21 | +import org.junit.Before; |
| 22 | +import org.junit.Test; |
| 23 | + |
| 24 | +@TestPlugin( |
| 25 | + name = "gerrit-plugin-qt-workflow", |
| 26 | + sysModule = "com.googlesource.gerrit.plugins.qtcodereview.QtModule", |
| 27 | + sshModule = "com.googlesource.gerrit.plugins.qtcodereview.QtSshModule" |
| 28 | +) |
| 29 | + |
| 30 | +@UseSsh |
| 31 | +public class QtEmailSendingIT extends QtCodeReviewIT { |
| 32 | + |
| 33 | + @Before |
| 34 | + public void grantPermissions() throws Exception { |
| 35 | + grant(project, "refs/heads/master", Permission.QT_STAGE, false, REGISTERED_USERS); |
| 36 | + grant(project, "refs/staging/*", Permission.PUSH, false, adminGroupUuid()); |
| 37 | + grant(project, "refs/builds/*", Permission.CREATE, false, adminGroupUuid()); |
| 38 | + |
| 39 | + ProjectConfig cfg = projectCache.get(project).getConfig(); |
| 40 | + Util.allow(cfg, Permission.forLabel("Code-Review"), -2, +2, REGISTERED_USERS, "refs/*"); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void stagedByOwnerBuildPass() throws Exception { |
| 45 | + PushOneCommit.Result c = pushCommit("master", "commitmsg1", "file1", "content1"); |
| 46 | + approve(c.getChangeId()); |
| 47 | + QtStage(c); |
| 48 | + QtNewBuild("master", "test_build_01"); |
| 49 | + sender.clear(); |
| 50 | + QtApproveBuild("master", "test_build_01"); |
| 51 | + |
| 52 | + FakeEmailSender.Message m = sender.getMessages(c.getChangeId(), "merged").get(0); |
| 53 | + Address expectedTo = new Address(user.fullName, user.email); |
| 54 | + assertThat(m.rcpt()).containsExactly(expectedTo); |
| 55 | + assertThat(((EmailHeader.AddressList) m.headers().get("To")).getAddressList()) |
| 56 | + .containsExactly(expectedTo); |
| 57 | + assertThat(m.body()).contains(c.getChangeId()); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void stagedByOwnerBuildFail() throws Exception { |
| 62 | + PushOneCommit.Result c = pushCommit("master", "commitmsg1", "file1", "content1"); |
| 63 | + approve(c.getChangeId()); |
| 64 | + QtStage(c); |
| 65 | + QtNewBuild("master", "test_build_02"); |
| 66 | + sender.clear(); |
| 67 | + QtFailBuild("master", "test_build_02"); |
| 68 | + |
| 69 | + FakeEmailSender.Message m = sender.getMessages(c.getChangeId(), "comment").get(0); |
| 70 | + Address expectedTo = new Address(user.fullName, user.email); |
| 71 | + assertThat(m.rcpt()).containsExactly(expectedTo); |
| 72 | + assertThat(((EmailHeader.AddressList) m.headers().get("To")).getAddressList()) |
| 73 | + .containsExactly(expectedTo); |
| 74 | + assertThat(m.body()).contains(c.getChangeId()); |
| 75 | + assertThat(m.body()).contains(BUILD_FAIL_MESSAGE); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void stagedByOtherBuildPass() throws Exception { |
| 80 | + PushOneCommit.Result c = pushCommit("master", "commitmsg1", "file1", "content1"); |
| 81 | + approve(c.getChangeId()); |
| 82 | + QtStageByAdmin(c); |
| 83 | + QtNewBuild("master", "test_build_03"); |
| 84 | + sender.clear(); |
| 85 | + QtApproveBuild("master", "test_build_03"); |
| 86 | + |
| 87 | + FakeEmailSender.Message m = sender.getMessages(c.getChangeId(), "merged").get(0); |
| 88 | + Address expectedTo = new Address(user.fullName, user.email); |
| 89 | + assertThat(m.rcpt()).containsExactly(expectedTo); |
| 90 | + assertThat(((EmailHeader.AddressList) m.headers().get("To")).getAddressList()) |
| 91 | + .containsExactly(expectedTo); |
| 92 | + assertThat(m.body()).contains(c.getChangeId()); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void stagedByOtherBuildFail() throws Exception { |
| 97 | + PushOneCommit.Result c = pushCommit("master", "commitmsg1", "file1", "content1"); |
| 98 | + approve(c.getChangeId()); |
| 99 | + QtStageByAdmin(c); |
| 100 | + QtNewBuild("master", "test_build_04"); |
| 101 | + sender.clear(); |
| 102 | + QtFailBuild("master", "test_build_04"); |
| 103 | + |
| 104 | + FakeEmailSender.Message m = sender.getMessages(c.getChangeId(), "comment").get(0); |
| 105 | + Address expectedTo = new Address(user.fullName, user.email); |
| 106 | + assertThat(m.rcpt()).containsExactly(expectedTo); |
| 107 | + assertThat(((EmailHeader.AddressList) m.headers().get("To")).getAddressList()) |
| 108 | + .containsExactly(expectedTo); |
| 109 | + assertThat(m.body()).contains(c.getChangeId()); |
| 110 | + assertThat(m.body()).contains(BUILD_FAIL_MESSAGE); |
| 111 | + } |
| 112 | + |
| 113 | + private void QtStageByAdmin(PushOneCommit.Result c) throws Exception { |
| 114 | + RestResponse response = call_REST_API_Stage_By_Admin(c.getChangeId(), c.getCommit().getName()); |
| 115 | + response.assertOK(); |
| 116 | + assertThat(c.getChange().change().getStatus()).isEqualTo(Change.Status.STAGED); |
| 117 | + } |
| 118 | + |
| 119 | + private RestResponse call_REST_API_Stage_By_Admin(String changeId, String revisionId) throws Exception { |
| 120 | + String url = "/changes/" + changeId + "/revisions/" + revisionId + "/gerrit-plugin-qt-workflow~stage"; |
| 121 | + RestResponse response = adminRestSession.post(url); |
| 122 | + return response; |
| 123 | + } |
| 124 | + |
| 125 | +} |
0 commit comments