File: notifier_spec.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (82 lines) | stat: -rw-r--r-- 2,839 bytes parent folder | download
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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe MicrosoftTeams::Notifier do
  subject { described_class.new(webhook_url) }

  let(:webhook_url) { 'https://example.gitlab.com/' }
  let(:header) { { 'Content-Type' => 'application/json' } }
  let(:options) do
    {
      title: 'JohnDoe4/project2',
      activity: {
        title: 'Issue opened by user6',
        subtitle: 'in [JohnDoe4/project2](http://localhost/namespace2/gitlabhq)',
        text: '[#1 Awesome issue](http://localhost/namespace2/gitlabhq/issues/1)',
        image: 'http://someimage.com'
      },
      attachments: "[GitLab](https://gitlab.com)\n\n- _Ruby_\n- **Go**\n"
    }
  end

  let(:body) do
    {
      "type" => "message",
      "attachments" => [
        {
          "contentType" => "application/vnd.microsoft.card.adaptive",
          "content" =>
          {
            "type" => "AdaptiveCard",
            msteams: { width: "Full" },
            "version" => "1.0",
            "body" => [
              {
                "type" => "TextBlock", "text" => "JohnDoe4/project2", "weight" => "bolder", "size" => "medium"
              },
              {
                "type" => "ColumnSet",
                "columns" => [
                  {
                    "type" => "Column", "width" => "auto", "items" => [{ "type" => "Image", "url" => "http://someimage.com", "size" => "medium" }]
                  },
                  {
                    "type" => "Column",
                    "width" => "stretch",
                    "items" => [
                      { "type" => "TextBlock", "text" => "Issue opened by user6", "weight" => "bolder", "wrap" => true },
                      { "type" => "TextBlock", "text" => "in [JohnDoe4/project2](http://localhost/namespace2/gitlabhq)", "isSubtle" => true, "wrap" => true },
                      { "type" => "TextBlock", "text" => "[#1 Awesome issue](http://localhost/namespace2/gitlabhq/issues/1)", "wrap" => true }
                    ]
                  }
                ]
              },
              { "type" => "TextBlock", "text" => "[GitLab](https://gitlab.com)\n\n- _Ruby_\n- **Go**\n", "wrap" => true }
            ]
          }
        }
      ]
    }
  end

  describe '#ping' do
    before do
      stub_request(:post, webhook_url).with(body: JSON(body), headers: { 'Content-Type' => 'application/json' }).to_return(status: 200, body: "", headers: {})
    end

    it 'expects to receive successful answer' do
      expect(subject.ping(options)).to be true
    end
  end

  describe '#body' do
    it 'returns Markdown-based body when HTML was passed' do
      expect(subject.send(:body, **options)).to eq(body.to_json)
    end

    it 'fails when empty Hash was passed' do
      expect { subject.send(:body, **{}) }.to raise_error(ArgumentError)
    end
  end
end