summaryrefslogtreecommitdiff
path: root/spec/models/project_services/chat_message/deployment_message_spec.rb
blob: 42c1689db3d1876289de24d6d97a04c70d247a77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# frozen_string_literal: true

require 'spec_helper'

describe ChatMessage::DeploymentMessage do
  describe '#pretext' do
    it 'returns a message with the data returned by the deployment data builder' do
      environment = create(:environment, name: "myenvironment")
      project = create(:project, :repository)
      commit = project.commit('HEAD')
      deployment = create(:deployment, status: :success, environment: environment, project: project, sha: commit.sha)
      data = Gitlab::DataBuilder::Deployment.build(deployment)

      message = described_class.new(data)

      expect(message.pretext).to eq("Deploy to myenvironment succeeded")
    end

    it 'returns a message for a successful deployment' do
      data = {
        status: 'success',
        environment: 'production'
      }

      message = described_class.new(data)

      expect(message.pretext).to eq('Deploy to production succeeded')
    end

    it 'returns a message for a failed deployment' do
      data = {
        status: 'failed',
        environment: 'production'
      }

      message = described_class.new(data)

      expect(message.pretext).to eq('Deploy to production failed')
    end

    it 'returns a message for a canceled deployment' do
      data = {
        status: 'canceled',
        environment: 'production'
      }

      message = described_class.new(data)

      expect(message.pretext).to eq('Deploy to production canceled')
    end

    it 'returns a message for a deployment to another environment' do
      data = {
        status: 'success',
        environment: 'staging'
      }

      message = described_class.new(data)

      expect(message.pretext).to eq('Deploy to staging succeeded')
    end

    it 'returns a message for a deployment with any other status' do
      data = {
        status: 'unknown',
        environment: 'staging'
      }

      message = described_class.new(data)

      expect(message.pretext).to eq('Deploy to staging unknown')
    end
  end

  describe '#attachments' do
    def deployment_data(params)
      {
        object_kind: "deployment",
        status: "success",
        deployable_id: 3,
        deployable_url: "deployable_url",
        environment: "sandbox",
        project: {
          name: "greatproject",
          web_url: "project_web_url",
          path_with_namespace: "project_path_with_namespace"
        },
        user: {
          name: "Jane Person",
          username: "jane"
        },
        user_url: "user_url",
        short_sha: "12345678",
        commit_url: "commit_url",
        commit_title: "commit title text"
      }.merge(params)
    end

    it 'returns attachments with the data returned by the deployment data builder' do
      user = create(:user, name: "John Smith", username: "smith")
      namespace = create(:namespace, name: "myspace")
      project = create(:project, :repository, namespace: namespace, name: "myproject")
      commit = project.commit('HEAD')
      environment = create(:environment, name: "myenvironment", project: project)
      ci_build = create(:ci_build, project: project)
      deployment = create(:deployment, :success, deployable: ci_build, environment: environment, project: project, user: user, sha: commit.sha)
      job_url = Gitlab::Routing.url_helpers.project_job_url(project, ci_build)
      commit_url = Gitlab::UrlBuilder.build(deployment.commit)
      user_url = Gitlab::Routing.url_helpers.user_url(user)
      data = Gitlab::DataBuilder::Deployment.build(deployment)

      message = described_class.new(data)

      expect(message.attachments).to eq([{
        text: "[myspace/myproject](#{project.web_url}) with job [##{ci_build.id}](#{job_url}) by [John Smith (smith)](#{user_url})\n[#{deployment.short_sha}](#{commit_url}): #{commit.title}",
        color: "good"
      }])
    end

    it 'returns attachments for a failed deployment' do
      data = deployment_data(status: 'failed')

      message = described_class.new(data)

      expect(message.attachments).to eq([{
        text: "[project_path_with_namespace](project_web_url) with job [#3](deployable_url) by [Jane Person (jane)](user_url)\n[12345678](commit_url): commit title text",
        color: "danger"
      }])
    end

    it 'returns attachments for a canceled deployment' do
      data = deployment_data(status: 'canceled')

      message = described_class.new(data)

      expect(message.attachments).to eq([{
        text: "[project_path_with_namespace](project_web_url) with job [#3](deployable_url) by [Jane Person (jane)](user_url)\n[12345678](commit_url): commit title text",
        color: "warning"
      }])
    end

    it 'uses a neutral color for a deployment with any other status' do
      data = deployment_data(status: 'some-new-status-we-make-in-the-future')

      message = described_class.new(data)

      expect(message.attachments).to eq([{
        text: "[project_path_with_namespace](project_web_url) with job [#3](deployable_url) by [Jane Person (jane)](user_url)\n[12345678](commit_url): commit title text",
        color: "#334455"
      }])
    end
  end
end