about summary refs log tree commit diff
path: root/spec/controllers/api/v1/emails/confirmations_controller_spec.rb
blob: d272ff38d2a1d2fc3c884108d404c7c5498f0ebb (plain) (blame)
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
require 'rails_helper'

RSpec.describe Api::V1::Emails::ConfirmationsController, type: :controller 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 'from a random app' do
        it 'returns http forbidden' do
          post :create
          expect(response).to have_http_status(403)
        end
      end

      context '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 'but 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
end