|
| 1 | +defmodule Notifications.Workers.CoordinatorTest do |
| 2 | + use Notifications.DataCase |
| 3 | + import Mock |
| 4 | + |
| 5 | + alias Notifications.Workers.Coordinator.PipelineFinished |
| 6 | + alias Notifications.Workers.Slack |
| 7 | + alias Notifications.Models.{Rule, Notification, Pattern} |
| 8 | + alias Notifications.Repo |
| 9 | + |
| 10 | + @pipeline_id Ecto.UUID.generate() |
| 11 | + @project_id Ecto.UUID.generate() |
| 12 | + @org_id Ecto.UUID.generate() |
| 13 | + @creator_id Ecto.UUID.generate() |
| 14 | + |
| 15 | + setup do |
| 16 | + mock_grpc_services() |
| 17 | + :ok |
| 18 | + end |
| 19 | + |
| 20 | + describe ".handle_message" do |
| 21 | + test "when notification creator has access to the project" do |
| 22 | + add_notification_with_rule_and_patterns() |
| 23 | + authorize_everything() |
| 24 | + |
| 25 | + with_mock Slack, publish: fn _, _, _, _ -> :ok end do |
| 26 | + pipeline_event = InternalApi.Plumber.PipelineEvent.new(pipeline_id: @pipeline_id) |
| 27 | + message = InternalApi.Plumber.PipelineEvent.encode(pipeline_event) |
| 28 | + |
| 29 | + assert PipelineFinished.handle_message(message) |
| 30 | + assert_called(Slack.publish(:_, :_, :_, :_)) |
| 31 | + end |
| 32 | + end |
| 33 | + |
| 34 | + test "when notification creator doesn't have access to the project" do |
| 35 | + add_notification_with_rule_and_patterns() |
| 36 | + |
| 37 | + with_mock Slack, publish: fn _, _, _, _ -> :error end do |
| 38 | + pipeline_event = InternalApi.Plumber.PipelineEvent.new(pipeline_id: @pipeline_id) |
| 39 | + message = InternalApi.Plumber.PipelineEvent.encode(pipeline_event) |
| 40 | + |
| 41 | + assert PipelineFinished.handle_message(message) |
| 42 | + assert_not_called(Slack.publish(:_, :_, :_, :_)) |
| 43 | + end |
| 44 | + end |
| 45 | + |
| 46 | + test "when notification has empty creator_id" do |
| 47 | + setup_notification_with_empty_creator() |
| 48 | + |
| 49 | + with_mock Slack, publish: fn _, _, _, _ -> :ok end do |
| 50 | + pipeline_event = InternalApi.Plumber.PipelineEvent.new(pipeline_id: @pipeline_id) |
| 51 | + message = InternalApi.Plumber.PipelineEvent.encode(pipeline_event) |
| 52 | + |
| 53 | + assert PipelineFinished.handle_message(message) |
| 54 | + assert_called(Slack.publish(:_, :_, :_, :_)) |
| 55 | + end |
| 56 | + end |
| 57 | + end |
| 58 | + |
| 59 | + ### |
| 60 | + ### Helper funcs |
| 61 | + ### |
| 62 | + |
| 63 | + defp authorize_everything do |
| 64 | + GrpcMock.stub( |
| 65 | + RBACMock, |
| 66 | + :list_user_permissions, |
| 67 | + fn _, _ -> |
| 68 | + InternalApi.RBAC.ListUserPermissionsResponse.new( |
| 69 | + permissions: ["organization.view", "project.view"] |
| 70 | + ) |
| 71 | + end |
| 72 | + ) |
| 73 | + end |
| 74 | + |
| 75 | + defp mock_grpc_services do |
| 76 | + alias InternalApi.{Plumber, Projecthub, RepoProxy, PlumberWF, Organization, ResponseStatus} |
| 77 | + |
| 78 | + GrpcMock.stub( |
| 79 | + PipelinesMock, |
| 80 | + :describe, |
| 81 | + fn _, _ -> |
| 82 | + Plumber.DescribeResponse.new( |
| 83 | + response_status: Plumber.ResponseStatus.new(code: :OK), |
| 84 | + pipeline: Plumber.Pipeline.new(project_id: @project_id) |
| 85 | + ) |
| 86 | + end |
| 87 | + ) |
| 88 | + |
| 89 | + GrpcMock.stub( |
| 90 | + ProjectServiceMock, |
| 91 | + :describe, |
| 92 | + fn _, _ -> |
| 93 | + project = |
| 94 | + Projecthub.Project.new( |
| 95 | + metadata: Projecthub.Project.Metadata.new(id: @project_id, org_id: @org_id) |
| 96 | + ) |
| 97 | + |
| 98 | + Projecthub.DescribeResponse.new( |
| 99 | + metadata: |
| 100 | + Projecthub.ResponseMeta.new( |
| 101 | + status: |
| 102 | + Projecthub.ResponseMeta.Status.new(code: Projecthub.ResponseMeta.Code.value(:OK)) |
| 103 | + ), |
| 104 | + project: project |
| 105 | + ) |
| 106 | + end |
| 107 | + ) |
| 108 | + |
| 109 | + GrpcMock.stub( |
| 110 | + RepoProxyMock, |
| 111 | + :describe, |
| 112 | + fn _, _ -> |
| 113 | + RepoProxy.DescribeResponse.new( |
| 114 | + status: ResponseStatus.new(code: ResponseStatus.Code.value(:OK)), |
| 115 | + hook: RepoProxy.Hook.new() |
| 116 | + ) |
| 117 | + end |
| 118 | + ) |
| 119 | + |
| 120 | + GrpcMock.stub( |
| 121 | + WorkflowMock, |
| 122 | + :describe, |
| 123 | + fn _, _ -> |
| 124 | + PlumberWF.DescribeResponse.new( |
| 125 | + status: ResponseStatus.new(code: ResponseStatus.Code.value(:OK)), |
| 126 | + workflow: PlumberWF.WorkflowDetails.new() |
| 127 | + ) |
| 128 | + end |
| 129 | + ) |
| 130 | + |
| 131 | + GrpcMock.stub( |
| 132 | + OrganizationMock, |
| 133 | + :describe, |
| 134 | + fn _, _ -> |
| 135 | + Organization.DescribeResponse.new( |
| 136 | + status: ResponseStatus.new(code: ResponseStatus.Code.value(:OK)), |
| 137 | + organization: Organization.Organization.new() |
| 138 | + ) |
| 139 | + end |
| 140 | + ) |
| 141 | + |
| 142 | + GrpcMock.stub( |
| 143 | + RBACMock, |
| 144 | + :list_user_permissions, |
| 145 | + fn _, _ -> |
| 146 | + InternalApi.RBAC.ListUserPermissionsResponse.new(permissions: []) |
| 147 | + end |
| 148 | + ) |
| 149 | + end |
| 150 | + |
| 151 | + defp setup_notification_with_empty_creator do |
| 152 | + notification = |
| 153 | + Repo.insert!(%Notification{ |
| 154 | + org_id: @org_id, |
| 155 | + creator_id: nil, |
| 156 | + name: "Test Notification Empty Creator", |
| 157 | + spec: %{"rules" => []} |
| 158 | + }) |
| 159 | + |
| 160 | + create_rule_with_patterns(notification) |
| 161 | + end |
| 162 | + |
| 163 | + defp add_notification_with_rule_and_patterns do |
| 164 | + notification = |
| 165 | + Repo.insert!(%Notification{ |
| 166 | + org_id: @org_id, |
| 167 | + creator_id: @creator_id, |
| 168 | + name: "Test Notification", |
| 169 | + spec: %{"rules" => []} |
| 170 | + }) |
| 171 | + |
| 172 | + create_rule_with_patterns(notification) |
| 173 | + end |
| 174 | + |
| 175 | + defp create_rule_with_patterns(notification) do |
| 176 | + rule = |
| 177 | + Repo.insert!(%Rule{ |
| 178 | + org_id: @org_id, |
| 179 | + notification_id: notification.id, |
| 180 | + name: "Test Rule", |
| 181 | + slack: %{ |
| 182 | + "endpoint" => "https://hooks.slack.com/services/TEST", |
| 183 | + "channels" => ["#test-channel"] |
| 184 | + }, |
| 185 | + email: %{}, |
| 186 | + webhook: %{} |
| 187 | + }) |
| 188 | + |
| 189 | + # Add patterns to the rule that will match our pipeline |
| 190 | + Repo.insert!(%Pattern{ |
| 191 | + org_id: @org_id, |
| 192 | + rule_id: rule.id, |
| 193 | + type: "project", |
| 194 | + regex: true, |
| 195 | + term: ".*" |
| 196 | + }) |
| 197 | + |
| 198 | + Repo.insert!(%Pattern{ |
| 199 | + org_id: @org_id, |
| 200 | + rule_id: rule.id, |
| 201 | + type: "branch", |
| 202 | + regex: true, |
| 203 | + term: ".*" |
| 204 | + }) |
| 205 | + |
| 206 | + Repo.insert!(%Pattern{ |
| 207 | + org_id: @org_id, |
| 208 | + rule_id: rule.id, |
| 209 | + type: "pipeline", |
| 210 | + regex: true, |
| 211 | + term: ".*" |
| 212 | + }) |
| 213 | + |
| 214 | + Repo.insert!(%Pattern{ |
| 215 | + org_id: @org_id, |
| 216 | + rule_id: rule.id, |
| 217 | + type: "result", |
| 218 | + regex: true, |
| 219 | + term: ".*" |
| 220 | + }) |
| 221 | + |
| 222 | + rule |
| 223 | + end |
| 224 | +end |
0 commit comments