Welcome to mirror list, hosted at ThFree Co, Russian Federation.

applications_spec.rb « api « requests « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 02dfbfa8fd733c58cdcb6da0c4c3b72a70213d40 (plain)
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
require 'spec_helper'

describe API::Applications, :api do
  include ApiHelpers

  let(:admin_user) { create(:user, admin: true) }
  let(:user) { create(:user, admin: false) }
  let(:application) { create(:application, name: 'application_name', redirect_uri: 'http://application.url', scopes: '') }

  describe 'POST /applications' do
    context 'authenticated and authorized user' do
      it 'creates and returns an OAuth application' do
        expect do
          post api('/applications', admin_user), name: 'application_name', redirect_uri: 'http://application.url', scopes: ''
        end.to change { Doorkeeper::Application.count }.by 1

        application = Doorkeeper::Application.find_by(name: 'application_name', redirect_uri: 'http://application.url')

        expect(response).to have_http_status 201
        expect(json_response).to be_a Hash
        expect(json_response['application_id']).to eq application.uid
        expect(json_response['secret']).to eq application.secret
        expect(json_response['callback_url']).to eq application.redirect_uri
      end

      it 'does not allow creating an application with the wrong redirect_uri format' do
        expect do
          post api('/applications', admin_user), name: 'application_name', redirect_uri: 'wrong_url_format', scopes: ''
        end.not_to change { Doorkeeper::Application.count }

        expect(response).to have_http_status 400
        expect(json_response).to be_a Hash
        expect(json_response['message']['redirect_uri'][0]).to eq('must be an absolute URI.')
      end

      it 'does not allow creating an application without a name' do
        expect do
          post api('/applications', admin_user), redirect_uri: 'http://application.url', scopes: ''
        end.not_to change { Doorkeeper::Application.count }

        expect(response).to have_http_status 400
        expect(json_response).to be_a Hash
        expect(json_response['error']).to eq('name is missing')
      end

      it 'does not allow creating an application without a redirect_uri' do
        expect do
          post api('/applications', admin_user), name: 'application_name', scopes: ''
        end.not_to change { Doorkeeper::Application.count }

        expect(response).to have_http_status 400
        expect(json_response).to be_a Hash
        expect(json_response['error']).to eq('redirect_uri is missing')
      end

      it 'does not allow creating an application without scopes' do
        expect do
          post api('/applications', admin_user), name: 'application_name', redirect_uri: 'http://application.url'
        end.not_to change { Doorkeeper::Application.count }

        expect(response).to have_http_status 400
        expect(json_response).to be_a Hash
        expect(json_response['error']).to eq('scopes is missing')
      end
    end

    context 'authorized user without authorization' do
      it 'does not create application' do
        expect do
          post api('/applications', user), name: 'application_name', redirect_uri: 'http://application.url', scopes: ''
        end.not_to change { Doorkeeper::Application.count }

        expect(response).to have_http_status 403
      end
    end

    context 'non-authenticated user' do
      it 'does not create application' do
        expect do
          post api('/applications'), name: 'application_name', redirect_uri: 'http://application.url'
        end.not_to change { Doorkeeper::Application.count }

        expect(response).to have_http_status 401
      end
    end
  end

  describe 'GET /applications' do
    context 'authenticated and authorized user' do
      it 'can list application' do
        get api('/applications')

        expect(response).to have_gitlab_http_status(200)
        expect(json_response).to be_a(Array)
      end
    end

    context 'non-authenticated user' do
      it 'cannot list application' do
        get api('/applications')

        expect(response).to have_http_status 401
      end
    end
  end

  describe 'DELETE /applications/:id' do
    context 'authenticated and authorized user' do
      it 'can delete an application' do
        delete api("/applications/#{application.id}")

        expect(response).to have_gitlab_http_status(204)
      end
    end

    context 'non-authenticated user' do
      it 'cannot delete an application' do
        delete api("/applications/#{application.id}")

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