summaryrefslogtreecommitdiff
path: root/spec/lib/mattermost/team_spec.rb
blob: 0870114ca28af0bdbebcd0f71fa9b518566dba0c (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Mattermost::Team do
  before do
    session = Mattermost::Session.new(nil)
    session.base_uri = 'http://mattermost.example.com'

    allow_any_instance_of(Mattermost::Client).to receive(:with_session)
      .and_yield(session)
  end

  describe '#all' do
    subject { described_class.new(nil).all }

    let(:test_team) do
      {
        "id" => "xiyro8huptfhdndadpz8r3wnbo",
        "create_at" => 1482174222155,
        "update_at" => 1482174222155,
        "delete_at" => 0,
        "display_name" => "chatops",
        "name" => "chatops",
        "email" => "admin@example.com",
        "type" => "O",
        "company_name" => "",
        "allowed_domains" => "",
        "invite_id" => "o4utakb9jtb7imctdfzbf9r5ro",
        "allow_open_invite" => false
      }
    end

    context 'for valid request' do
      let(:response) { [test_team] }

      before do
        stub_request(:get, 'http://mattermost.example.com/api/v4/users/me/teams')
          .to_return(
            status: 200,
            headers: { 'Content-Type' => 'application/json' },
            body: response.to_json
          )
      end

      it 'returns teams' do
        is_expected.to eq(response)
      end
    end

    context 'for error message' do
      before do
        stub_request(:get, 'http://mattermost.example.com/api/v4/users/me/teams')
          .to_return(
            status: 500,
            headers: { 'Content-Type' => 'application/json' },
            body: {
              id: 'api.team.list.app_error',
              message: 'Cannot list teams.',
              detailed_error: '',
              request_id: 'obc374man7bx5r3dbc1q5qhf3r',
              status_code: 500
            }.to_json
          )
      end

      it 'raises an error with message' do
        expect { subject }.to raise_error(Mattermost::Error, 'Cannot list teams.')
      end
    end
  end

  describe '#create' do
    subject { described_class.new(nil).create(name: "devteam", display_name: "Dev Team", type: "O") }

    context 'for a new team' do
      let(:response) do
        {
          "id" => "cuojfcetjty7tb4pxe47pwpndo",
          "create_at" => 1517688728701,
          "update_at" => 1517688728701,
          "delete_at" => 0,
          "display_name" => "Dev Team",
          "name" => "devteam",
          "description" => "",
          "email" => "admin@example.com",
          "type" => "O",
          "company_name" => "",
          "allowed_domains" => "",
          "invite_id" => "7mp9d3ayaj833ymmkfnid8js6w",
          "allow_open_invite" => false
        }
      end

      before do
        stub_request(:post, "http://mattermost.example.com/api/v4/teams")
          .to_return(
            status: 201,
            body: response.to_json,
            headers: { 'Content-Type' => 'application/json' }
          )
      end

      it 'returns the new team' do
        is_expected.to eq(response)
      end
    end

    context 'for existing team' do
      before do
        stub_request(:post, 'http://mattermost.example.com/api/v4/teams')
          .to_return(
            status: 400,
            headers: { 'Content-Type' => 'application/json' },
            body: {
                id: "store.sql_team.save.domain_exists.app_error",
                message: "A team with that name already exists",
                detailed_error: "",
                request_id: "1hsb5bxs97r8bdggayy7n9gxaw",
                status_code: 400
            }.to_json
          )
      end

      it 'raises an error with message' do
        expect { subject }.to raise_error(Mattermost::Error, 'A team with that name already exists')
      end
    end
  end

  describe '#delete' do
    subject { described_class.new(nil).destroy(team_id: "cuojfcetjty7tb4pxe47pwpndo") }

    context 'for an existing team' do
      let(:response) do
        {
            "status" => "OK"
        }
      end

      before do
        stub_request(:delete, "http://mattermost.example.com/api/v4/teams/cuojfcetjty7tb4pxe47pwpndo")
          .to_return(
            status: 200,
            body: response.to_json,
            headers: { 'Content-Type' => 'application/json' }
          )
      end

      it 'returns team status' do
        is_expected.to eq(response)
      end
    end

    context 'for an unknown team' do
      before do
        stub_request(:delete, "http://mattermost.example.com/api/v4/teams/cuojfcetjty7tb4pxe47pwpndo")
          .to_return(
            status: 404,
            body: {
              id: "store.sql_team.get.find.app_error",
              message: "We couldn't find the existing team",
              detailed_error: "",
              request_id: "my114ab5nbnui8c9pes4kz8mza",
              status_code: 404
            }.to_json,
            headers: { 'Content-Type' => 'application/json' }
          )
      end

      it 'raises an error with message' do
        expect { subject }.to raise_error(Mattermost::Error, "We couldn't find the existing team")
      end
    end
  end
end