-
Notifications
You must be signed in to change notification settings - Fork 195
/
Copy pathswagger_spec.rb
146 lines (145 loc) · 5.44 KB
/
swagger_spec.rb
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
require 'spec_helper'
describe 'Swagger' do
context 'swaggerUi' do
before do
visit '/swagger'
end
it 'loads foos resource' do
expect(page).to have_css 'li#resource_foos'
end
it 'loads Swagger UI' do
expect(page.evaluate_script('window.swaggerUi != null')).to be true
end
end
context '#options' do
before do
@options = GrapeSwaggerRails.options.dup
end
context '#headers' do
before do
GrapeSwaggerRails.options.headers['X-Test-Header'] = 'Test Value'
GrapeSwaggerRails.options.headers['X-Another-Header'] = 'Another Value'
visit '/swagger'
end
it 'adds headers' do
find('#endpointListTogger_headers', visible: true).click
first('a[href="#!/headers/GET_api_headers_format"]', visible: true).click
click_button 'Try it out!'
expect(page).to have_css 'span.attribute', text: 'X-Test-Header'
expect(page).to have_css 'span.string', text: 'Test Value'
end
it 'supports multiple headers' do
find('#endpointListTogger_headers', visible: true).click
first('a[href="#!/headers/GET_api_headers_format"]', visible: true).click
click_button 'Try it out!'
expect(page).to have_css 'span.attribute', text: 'X-Test-Header'
expect(page).to have_css 'span.string', text: 'Test Value'
expect(page).to have_css 'span.attribute', text: 'X-Another-Header'
expect(page).to have_css 'span.string', text: 'Another Value'
end
end
context '#api_auth:basic' do
before do
GrapeSwaggerRails.options.api_auth = 'basic'
GrapeSwaggerRails.options.api_key_name = 'Authorization'
GrapeSwaggerRails.options.api_key_type = 'header'
visit '/swagger'
end
it 'adds an Authorization header' do
page.execute_script("$('#input_apiKey').val('username:password')")
page.execute_script("$('#input_apiKey').trigger('change')")
find('#endpointListTogger_headers', visible: true).click
first('a[href="#!/headers/GET_api_headers_format"]', visible: true).click
click_button 'Try it out!'
expect(page).to have_css 'span.attribute', text: 'Authorization'
expect(page).to have_css 'span.string', text: "Basic #{Base64.encode64('username:password').strip}"
end
end
context '#api_auth:bearer' do
before do
GrapeSwaggerRails.options.api_auth = 'bearer'
GrapeSwaggerRails.options.api_key_name = 'Authorization'
GrapeSwaggerRails.options.api_key_type = 'header'
visit '/swagger'
end
it 'adds an Authorization header' do
page.execute_script("$('#input_apiKey').val('token')")
page.execute_script("$('#input_apiKey').trigger('change')")
find('#endpointListTogger_headers', visible: true).click
first('a[href="#!/headers/GET_api_headers_format"]', visible: true).click
click_button 'Try it out!'
expect(page).to have_css 'span.attribute', text: 'Authorization'
expect(page).to have_css 'span.string', text: 'Bearer token'
end
end
context '#api_auth:token' do
before do
GrapeSwaggerRails.options.api_key_name = 'api_token'
GrapeSwaggerRails.options.api_key_type = 'query'
visit '/swagger'
end
it 'adds an api_token query parameter' do
page.execute_script("$('#input_apiKey').val('dummy')")
page.execute_script("$('#input_apiKey').trigger('change')")
find('#endpointListTogger_params', visible: true).click
first('a[href="#!/params/GET_api_params_format"]', visible: true).click
click_button 'Try it out!'
expect(page).to have_css 'span.attribute', text: 'api_token'
expect(page).to have_css 'span.string', text: 'dummy'
end
end
context '#api_auth:oauth2' do
before do
GrapeSwaggerRails.options.api_auth = 'bearer'
GrapeSwaggerRails.options.api_key_name = 'Authorization'
GrapeSwaggerRails.options.api_key_type = 'header'
visit '/swagger'
end
it 'adds a token when the route specifies oauth2 authorization' do
page.execute_script("$('#input_apiKey').val('token')")
page.execute_script("$('#input_apiKey').trigger('change')")
find('#endpointListTogger_oauth2', visible: true).click
first('a[href="#!/oauth2/GET_api_oauth2_format"]', visible: true).click
click_button 'Try it out!'
expect(page).to have_css 'span.attribute', text: 'Authorization'
expect(page).to have_css 'span.string', text: 'Bearer token'
end
end
context '#before_filter' do
before do
GrapeSwaggerRails.options.before_filter do |_request|
flash[:error] = 'Unauthorized Access'
redirect_to '/'
false
end
visit '/swagger'
end
it 'denies access' do
expect(current_path).to eq '/'
expect(page).to have_content 'Unauthorized Access'
end
end
context '#app_name' do
context 'set' do
before do
GrapeSwaggerRails.options.app_name = 'Test App'
visit '/swagger'
end
it 'sets page title' do
expect(page.title).to eq 'Test App'
end
end
context 'not set' do
before do
visit '/swagger'
end
it 'defaults page title' do
expect(page.title).to eq 'Swagger'
end
end
end
after do
GrapeSwaggerRails.options = @options
end
end
end