~cytrogen/masto-fe

ref: 0e1d2e15a3a4b73c26cb7b06b47db39d08855529 masto-fe/spec/controllers/api/v1/emails/confirmations_controller_spec.rb -rw-r--r-- 3.6 KiB
0e1d2e15 — Claire Merge pull request #2302 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
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Api::V1::Emails::ConfirmationsController do
  let(:confirmed_at) { nil }
  let(:user)         { Fabricate(:user, confirmed_at: confirmed_at) }
  let(:app)          { Fabricate(:application) }
  let(:token)        { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes, application: app) }
  let(:scopes)       { 'write' }

  describe '#create' do
    context 'with an oauth token' do
      before do
        allow(controller).to receive(:doorkeeper_token) { token }
      end

      context 'when from a random app' do
        it 'returns http forbidden' do
          post :create
          expect(response).to have_http_status(403)
        end
      end

      context 'when from an app that created the account' 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
            post :create
            expect(response).to have_http_status(403)
          end

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

            it 'returns http success' do
              post :create
              expect(response).to have_http_status(:success)
            end
          end
        end

        context 'when the account is unconfirmed' do
          it 'returns http success' do
            post :create
            expect(response).to have_http_status(:success)
          end
        end
      end
    end

    context 'without an oauth token' do
      it 'returns http unauthorized' do
        post :create
        expect(response).to have_http_status(401)
      end
    end
  end

  describe '#check' do
    let(:scopes) { 'read' }

    context 'with an oauth token' do
      before do
        allow(controller).to receive(:doorkeeper_token) { token }
      end

      context 'when the account is not confirmed' do
        it 'returns http success' do
          get :check
          expect(response).to have_http_status(200)
        end

        it 'returns false' do
          get :check
          expect(body_as_json).to be false
        end
      end

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

        it 'returns http success' do
          get :check
          expect(response).to have_http_status(200)
        end

        it 'returns true' do
          get :check
          expect(body_as_json).to be true
        end
      end
    end

    context 'with an authentication cookie' do
      before do
        sign_in user, scope: :user
      end

      context 'when the account is not confirmed' do
        it 'returns http success' do
          get :check
          expect(response).to have_http_status(200)
        end

        it 'returns false' do
          get :check
          expect(body_as_json).to be false
        end
      end

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

        it 'returns http success' do
          get :check
          expect(response).to have_http_status(200)
        end

        it 'returns true' do
          get :check
          expect(body_as_json).to be true
        end
      end
    end

    context 'without an oauth token and an authentication cookie' do
      it 'returns http unauthorized' do
        get :check

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