Skip to content

Commit b234ea1

Browse files
committed
use a shared example instead of method in spec_helper
1 parent c2fdf64 commit b234ea1

File tree

7 files changed

+53
-55
lines changed

7 files changed

+53
-55
lines changed

spec/controllers/api/swagger_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'spec_helper'
22

33
describe 'Swagger API V1 docs', type: :request do
4-
describe 'all the paths' do
4+
describe 'all the paths' do
55
it "responds with swagger for all the apis" do
66
get '/api/v1/swagger_doc'
77
result = JSON(response.body)

spec/controllers/api/v1/me_spec.rb

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,38 @@
11
require 'spec_helper'
22

33
describe 'Me API V1', type: :request do
4+
include_context :doorkeeper_app_with_token
45
describe 'current' do
5-
it "Sends correct error code when no user present" do
6+
it 'Sends correct error code when no user present' do
67
get '/api/v1/me'
78
expect(response.response_code).to eq(401)
89
expect(response.body.match('OAuth')).to be_present
910
expect(response.headers['Content-Type'].match('json')).to be_present
1011
expect(response.headers['Access-Control-Allow-Origin']).to eq('*')
1112
expect(response.headers['Access-Control-Request-Method']).to eq('*')
1213
end
13-
14-
it "fails if no access token" do
14+
15+
it 'fails if no access token' do
1516
get '/api/v1/me', format: :json
1617
expect(response.response_code).to eq(401)
17-
expect(JSON(response.body)["error"].present?).to be_truthy
18+
expect(JSON(response.body)['error'].present?).to be_truthy
1819
expect(response.headers['Access-Control-Allow-Origin']).to eq('*')
1920
expect(response.headers['Access-Control-Request-Method']).to eq('*')
2021
end
2122

22-
it "responds with the user" do
23-
create_doorkeeper_app(scopes: OAUTH_SCOPES_S)
24-
get '/api/v1/me', format: :json, access_token: @token.token
23+
it 'responds with the user' do
24+
get '/api/v1/me', format: :json, access_token: access_token.token
2525
result = JSON.parse(response.body)
2626
expect(response.headers['Access-Control-Allow-Origin']).to eq('*')
27-
expect(result['user']['name']).to eq(@user.name)
28-
expect(result['user']['email']).to eq(@user.email)
27+
expect(result['user']['name']).to eq(user.name)
28+
expect(result['user']['email']).to eq(user.email)
2929
expect(response.response_code).to eq(200)
3030
end
3131
end
3232

33-
describe 'items' do
34-
it "gets items, returning correct pagination serialized by the serializer" do
35-
create_doorkeeper_app(scopes: OAUTH_SCOPES_S)
36-
get '/api/v1/me/items', format: :json, access_token: @token.token
33+
describe 'items' do
34+
it 'gets items, returning correct pagination serialized by the serializer' do
35+
get '/api/v1/me/items', format: :json, access_token: access_token.token
3736
result = JSON.parse(response.body)
3837

3938
expect(result['me'][0].keys.include?('secret')).to_not be_present
@@ -43,45 +42,42 @@
4342
end
4443
end
4544

46-
describe 'update current user' do
45+
describe 'update current user' do
46+
let(:attribs) { { email: 'foo@bar.com', name: 'new namething', access_token: access_token.token } }
47+
4748
context 'incorrectly scoped access token' do
4849
it "fails if the access token doesn't have the required scope" do
49-
create_doorkeeper_app(scopes: 'read_user')
50-
orig_name = @user.name
51-
attribs = {email: 'foo@bar.com', name: 'new namething', access_token: @token.token}
50+
access_token.update_attribute :scopes, 'read_user'
51+
orig_name = user.name
5252
put '/api/v1/me', attribs, format: :json
5353

5454
expect(response.response_code).to eq(403)
5555
expect(response.body.match(/OAuth error.* write to user/i)).to be_present
56-
@user.reload
57-
expect(@user.name).to eq(orig_name)
56+
user.reload
57+
expect(user.name).to eq(orig_name)
5858
end
5959
end
6060
context 'scoped access token' do
61-
it "updates the user" do
62-
create_doorkeeper_app(scopes: 'write_user')
63-
orig_name = @user.name
64-
attribs = {email: 'foo@bar.com', name: 'new namething', access_token: @token.token}
61+
before do
62+
access_token.update_attribute :scopes, 'write_user'
63+
end
64+
it 'updates the user' do
6565
put '/api/v1/me', attribs, format: :json
66-
@user.reload
67-
result = JSON.parse(response.body)
68-
6966
expect(response.response_code).to eq(200)
70-
expect(@user.name).to eq('new namething')
71-
expect(@user.email).to eq('foo@bar.com')
67+
user.reload
68+
expect(user.name).to eq('new namething')
69+
expect(user.email).to eq('foo@bar.com')
7270
end
7371

74-
it "fails when one of the supplied values isn't in the params" do
75-
create_doorkeeper_app(scopes: 'write_user')
76-
orig_name = @user.name
77-
attribs = {demo_value: 'foo', name: 'other', access_token: @token.token}
78-
put '/api/v1/me', attribs, format: :json
72+
it "fails when one of the supplied values isn't in the params" do
73+
orig_name = user.name
74+
put '/api/v1/me', attribs.merge(demo_value: 'foo'), format: :json
7975

8076
expect(response.response_code).to eq(400)
8177
expect(response.body).to match('demo_value does not have a valid value')
82-
@user.reload
83-
expect(@user.name).to_not eq('other')
78+
user.reload
79+
expect(user.name).to eq orig_name
8480
end
8581
end
8682
end
87-
end
83+
end

spec/controllers/documentation_controller_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
describe DocumentationController do
44

5-
describe 'view documentation index' do
6-
before do
5+
describe 'view documentation index' do
6+
before do
77
get :index
88
end
99
it { should respond_with(:success) }

spec/controllers/landing_controller_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
describe LandingController do
44

5-
describe 'view landing index' do
6-
before do
5+
describe 'view landing index' do
6+
before do
77
get :index
88
end
99
it { should respond_with(:success) }

spec/factories/default_factory.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,14 @@
1313
email { generate(:unique_email) }
1414
end
1515

16-
end
16+
factory :doorkeeper_application, class: Doorkeeper::Application do
17+
name { generate(:unique_name) }
18+
redirect_uri 'https://app.com'
19+
end
20+
21+
factory :doorkeeper_token, class: Doorkeeper::AccessToken do
22+
application_id { FactoryGirl.create(:doorkeeper_application).id }
23+
resource_owner_id { FactoryGirl.create(:user).id }
24+
scopes :public
25+
end
26+
end

spec/spec_helper.rb

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,3 @@
3333

3434
config.infer_spec_type_from_file_location!
3535
end
36-
37-
38-
def create_doorkeeper
39-
@user = FactoryGirl.create(:user)
40-
@application = Doorkeeper::Application.create(name: "MyApp", redirect_uri: "https://app.com")
41-
end
42-
43-
def create_doorkeeper_app(opts={})
44-
create_doorkeeper
45-
scopes = opts && opts[:scopes] || 'public'
46-
@token = Doorkeeper::AccessToken.create!(application_id: @application.id,
47-
resource_owner_id: @user.id, scopes: scopes)
48-
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
shared_context :doorkeeper_app_with_token do
2+
let(:doorkeeper_application) { FactoryGirl.create(:doorkeeper_application) }
3+
let(:user) { FactoryGirl.create(:user) }
4+
let(:access_token) { FactoryGirl.create(:doorkeeper_token, application_id: doorkeeper_application.id, resource_owner_id: user.id) }
5+
end

0 commit comments

Comments
 (0)