~cytrogen/masto-fe

ref: 48ef7d0f067943b5e608eaee7bba4e277bea83db masto-fe/spec/requests/api/v1/lists_spec.rb -rw-r--r-- 5.9 KiB
48ef7d0f — renovate[bot] Update libretranslate/libretranslate Docker tag to v1.3.12 (#27214) 2 years ago
                                                                                
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe 'Lists' do
  let(:user)    { Fabricate(:user) }
  let(:token)   { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
  let(:scopes)  { 'read:lists write:lists' }
  let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }

  describe 'GET /api/v1/lists' do
    subject do
      get '/api/v1/lists', headers: headers
    end

    let!(:lists) do
      [
        Fabricate(:list, account: user.account, title: 'first list', replies_policy: :followed),
        Fabricate(:list, account: user.account, title: 'second list', replies_policy: :list),
        Fabricate(:list, account: user.account, title: 'third list', replies_policy: :none),
        Fabricate(:list, account: user.account, title: 'fourth list', exclusive: true),
      ]
    end

    let(:expected_response) do
      lists.map do |list|
        {
          id: list.id.to_s,
          title: list.title,
          replies_policy: list.replies_policy,
          exclusive: list.exclusive,
        }
      end
    end

    before do
      Fabricate(:list)
    end

    it_behaves_like 'forbidden for wrong scope', 'write write:lists'

    it 'returns http success' do
      subject

      expect(response).to have_http_status(200)
    end

    it 'returns the expected lists' do
      subject

      expect(body_as_json).to match_array(expected_response)
    end
  end

  describe 'GET /api/v1/lists/:id' do
    subject do
      get "/api/v1/lists/#{list.id}", headers: headers
    end

    let(:list) { Fabricate(:list, account: user.account) }

    it_behaves_like 'forbidden for wrong scope', 'write write:lists'

    it 'returns http success' do
      subject

      expect(response).to have_http_status(200)
    end

    it 'returns the requested list correctly' do
      subject

      expect(body_as_json).to eq({
        id: list.id.to_s,
        title: list.title,
        replies_policy: list.replies_policy,
        exclusive: list.exclusive,
      })
    end

    context 'when the list belongs to a different user' do
      let(:list) { Fabricate(:list) }

      it 'returns http not found' do
        subject

        expect(response).to have_http_status(404)
      end
    end

    context 'when the list does not exist' do
      it 'returns http not found' do
        get '/api/v1/lists/-1', headers: headers

        expect(response).to have_http_status(404)
      end
    end
  end

  describe 'POST /api/v1/lists' do
    subject do
      post '/api/v1/lists', headers: headers, params: params
    end

    let(:params) { { title: 'my list', replies_policy: 'none', exclusive: 'true' } }

    it_behaves_like 'forbidden for wrong scope', 'read read:lists'

    it 'returns http success' do
      subject

      expect(response).to have_http_status(200)
    end

    it 'returns the new list' do
      subject

      expect(body_as_json).to match(a_hash_including(title: 'my list', replies_policy: 'none', exclusive: true))
    end

    it 'creates a list' do
      subject

      expect(List.where(account: user.account).count).to eq(1)
    end

    context 'when a title is not given' do
      let(:params) { { title: '' } }

      it 'returns http unprocessable entity' do
        subject

        expect(response).to have_http_status(422)
      end
    end

    context 'when the given replies_policy is invalid' do
      let(:params) { { title: 'a list', replies_policy: 'whatever' } }

      it 'returns http unprocessable entity' do
        subject

        expect(response).to have_http_status(422)
      end
    end
  end

  describe 'PUT /api/v1/lists/:id' do
    subject do
      put "/api/v1/lists/#{list.id}", headers: headers, params: params
    end

    let(:list)   { Fabricate(:list, account: user.account, title: 'my list') }
    let(:params) { { title: 'list', replies_policy: 'followed', exclusive: 'true' } }

    it_behaves_like 'forbidden for wrong scope', 'read read:lists'

    it 'returns http success' do
      subject

      expect(response).to have_http_status(200)
    end

    it 'returns the updated list' do
      subject

      list.reload

      expect(body_as_json).to eq({
        id: list.id.to_s,
        title: list.title,
        replies_policy: list.replies_policy,
        exclusive: list.exclusive,
      })
    end

    it 'updates the list title' do
      expect { subject }.to change { list.reload.title }.from('my list').to('list')
    end

    it 'updates the list replies_policy' do
      expect { subject }.to change { list.reload.replies_policy }.from('list').to('followed')
    end

    it 'updates the list exclusive' do
      expect { subject }.to change { list.reload.exclusive }.from(false).to(true)
    end

    context 'when the list does not exist' do
      it 'returns http not found' do
        put '/api/v1/lists/-1', headers: headers, params: params

        expect(response).to have_http_status(404)
      end
    end

    context 'when the list belongs to another user' do
      let(:list) { Fabricate(:list) }

      it 'returns http not found' do
        subject

        expect(response).to have_http_status(404)
      end
    end
  end

  describe 'DELETE /api/v1/lists/:id' do
    subject do
      delete "/api/v1/lists/#{list.id}", headers: headers
    end

    let(:list) { Fabricate(:list, account: user.account) }

    it_behaves_like 'forbidden for wrong scope', 'read read:lists'

    it 'returns http success' do
      subject

      expect(response).to have_http_status(200)
    end

    it 'deletes the list' do
      subject

      expect(List.where(id: list.id)).to_not exist
    end

    context 'when the list does not exist' do
      it 'returns http not found' do
        delete '/api/v1/lists/-1', headers: headers

        expect(response).to have_http_status(404)
      end
    end

    context 'when the list belongs to another user' do
      let(:list) { Fabricate(:list) }

      it 'returns http not found' do
        subject

        expect(response).to have_http_status(404)
      end
    end
  end
end