~cytrogen/masto-fe

ref: 1a33f1df170c6a3dfd4c6c55741e1e62727ec9ae masto-fe/spec/requests/api/v1/emails/confirmations_spec.rb -rw-r--r-- 4.4 KiB
1a33f1df — Claire Merge pull request #2305 from ClearlyClaire/glitch-soc/merge-upstream 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
# frozen_string_literal: true

require 'rails_helper'

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

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

    let(:params) { {} }

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

    context 'with an oauth token' do
      context 'when user was created by a different application' do
        let(:user) { Fabricate(:user, confirmed_at: confirmed_at, created_by_application: Fabricate(:application)) }

        it 'returns http forbidden' do
          subject

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

      context 'when user was created by the same application' do
        before do
          user.update(created_by_application: token.application)
        end

        context 'when the account is already confirmed' do
          let(:confirmed_at) { Time.now.utc }

          it 'returns http forbidden' do
            subject

            expect(response).to have_http_status(403)
          end

          context 'when user changed e-mail and has not confirmed it' do
            before do
              user.update(email: 'foo@bar.com')
            end

            it 'returns http success' do
              subject

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

        context 'when the account is unconfirmed' do
          it 'returns http success' do
            subject

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

        context 'with email param' do
          let(:params) { { email: 'foo@bar.com' } }

          it "updates the user's e-mail address", :aggregate_failures do
            subject

            expect(response).to have_http_status(200)
            expect(user.reload.unconfirmed_email).to eq('foo@bar.com')
          end
        end

        context 'with invalid email param' do
          let(:params) { { email: 'invalid' } }

          it 'returns http unprocessable entity' do
            subject

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

    context 'without an oauth token' do
      let(:headers) { {} }

      it 'returns http unauthorized' do
        subject

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

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

    it_behaves_like 'forbidden for wrong scope', 'write'

    context 'with an oauth token' do
      context 'when the account is not confirmed' do
        it 'returns the confirmation status successfully', :aggregate_failures do
          subject

          expect(response).to have_http_status(200)
          expect(body_as_json).to be false
        end
      end

      context 'when the account is confirmed' do
        let(:confirmed_at) { Time.now.utc }

        it 'returns the confirmation status successfully', :aggregate_failures do
          subject

          expect(response).to have_http_status(200)
          expect(body_as_json).to be true
        end
      end
    end

    context 'with an authentication cookie' do
      let(:headers) { {} }

      before do
        sign_in user, scope: :user
      end

      context 'when the account is not confirmed' do
        it 'returns the confirmation status successfully', :aggregate_failures do
          subject

          expect(response).to have_http_status(200)
          expect(body_as_json).to be false
        end
      end

      context 'when the account is confirmed' do
        let(:confirmed_at) { Time.now.utc }

        it 'returns the confirmation status successfully', :aggregate_failures do
          subject

          expect(response).to have_http_status(200)
          expect(body_as_json).to be true
        end
      end
    end

    context 'without an oauth token and an authentication cookie' do
      let(:headers) { {} }

      it 'returns http unauthorized' do
        subject

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