about summary refs log tree commit diff
path: root/spec/controllers/api/v1/apps_controller_spec.rb
blob: bde132c52e2485f78d1e052b0f3d6a7219c7e175 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Api::V1::AppsController, type: :controller do
  render_views

  describe 'POST #create' do
    let(:client_name) { 'Test app' }
    let(:scopes) { nil }
    let(:redirect_uris) { 'urn:ietf:wg:oauth:2.0:oob' }
    let(:website) { nil }

    let(:app_params) do
      {
        client_name: client_name,
        redirect_uris: redirect_uris,
        scopes: scopes,
        website: website,
      }
    end

    before do
      post :create, params: app_params
    end

    context 'with valid params' do
      it 'returns http success' do
        expect(response).to have_http_status(200)
      end

      it 'creates an OAuth app' do
        expect(Doorkeeper::Application.find_by(name: client_name)).to_not be_nil
      end

      it 'returns client ID and client secret' do
        json = body_as_json

        expect(json[:client_id]).to_not be_blank
        expect(json[:client_secret]).to_not be_blank
      end
    end

    context 'with an unsupported scope' do
      let(:scopes) { 'hoge' }

      it 'returns http unprocessable entity' do
        expect(response).to have_http_status(422)
      end
    end

    context 'with many duplicate scopes' do
      let(:scopes) { (%w(read) * 40).join(' ') }

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

      it 'only saves the scope once' do
        expect(Doorkeeper::Application.find_by(name: client_name).scopes.to_s).to eq 'read'
      end
    end

    context 'with a too-long name' do
      let(:client_name) { 'hoge' * 20 }

      it 'returns http unprocessable entity' do
        expect(response).to have_http_status(422)
      end
    end

    context 'with a too-long website' do
      let(:website) { "https://foo.bar/#{'hoge' * 2_000}" }

      it 'returns http unprocessable entity' do
        expect(response).to have_http_status(422)
      end
    end

    context 'with a too-long redirect_uris' do
      let(:redirect_uris) { "https://foo.bar/#{'hoge' * 2_000}" }

      it 'returns http unprocessable entity' do
        expect(response).to have_http_status(422)
      end
    end
  end
end