Skip to content

Commit

Permalink
Convert client applications fixtures into a factory
Browse files Browse the repository at this point in the history
  • Loading branch information
gravitystorm authored and tomhughes committed Jan 26, 2017
1 parent e1cac81 commit 8a6e8ae
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 81 deletions.
23 changes: 14 additions & 9 deletions test/controllers/oauth_clients_controller_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "test_helper"

class OauthClientsControllerTest < ActionController::TestCase
fixtures :users, :client_applications
fixtures :users

##
# test all routes which lead to this controller
Expand Down Expand Up @@ -38,6 +38,7 @@ def test_routes

def test_index
user = users(:public_user)
create_list(:client_application, 2, :user => user)

get :index, :display_name => user.display_name
assert_response :redirect
Expand Down Expand Up @@ -104,13 +105,14 @@ def test_create

def test_show
user = users(:public_user)
client = client_applications(:oauth_web_app)
client = create(:client_application, :user => user)
other_client = create(:client_application)

get :show, :display_name => user.display_name, :id => client.id
assert_response :redirect
assert_redirected_to login_path(:referer => oauth_client_path(:display_name => user.display_name, :id => client.id))

get :show, { :display_name => user.display_name, :id => client_applications(:normal_user_app).id }, { :user => user }
get :show, { :display_name => user.display_name, :id => other_client.id }, { :user => user }
assert_response :not_found
assert_template "not_found"

Expand All @@ -121,13 +123,14 @@ def test_show

def test_edit
user = users(:public_user)
client = client_applications(:oauth_web_app)
client = create(:client_application, :user => user)
other_client = create(:client_application)

get :edit, :display_name => user.display_name, :id => client.id
assert_response :redirect
assert_redirected_to login_path(:referer => edit_oauth_client_path(:display_name => user.display_name, :id => client.id))

get :edit, { :display_name => user.display_name, :id => client_applications(:normal_user_app).id }, { :user => user }
get :edit, { :display_name => user.display_name, :id => other_client.id }, { :user => user }
assert_response :not_found
assert_template "not_found"

Expand All @@ -147,12 +150,13 @@ def test_edit

def test_update
user = users(:public_user)
client = client_applications(:oauth_web_app)
client = create(:client_application, :user => user)
other_client = create(:client_application)

put :update, :display_name => user.display_name, :id => client.id
assert_response :forbidden

put :update, { :display_name => user.display_name, :id => client_applications(:normal_user_app).id }, { :user => user }
put :update, { :display_name => user.display_name, :id => other_client.id }, { :user => user }
assert_response :not_found
assert_template "not_found"

Expand Down Expand Up @@ -181,15 +185,16 @@ def test_update

def test_destroy
user = users(:public_user)
client = client_applications(:oauth_web_app)
client = create(:client_application, :user => user)
other_client = create(:client_application)

assert_difference "ClientApplication.count", 0 do
delete :destroy, :display_name => user.display_name, :id => client.id
end
assert_response :forbidden

assert_difference "ClientApplication.count", 0 do
delete :destroy, { :display_name => user.display_name, :id => client_applications(:normal_user_app).id }, { :user => user }
delete :destroy, { :display_name => user.display_name, :id => other_client.id }, { :user => user }
end
assert_response :not_found
assert_template "not_found"
Expand Down
4 changes: 2 additions & 2 deletions test/controllers/site_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ class SiteControllerTest < ActionController::TestCase
##
# setup oauth keys
def setup
Object.const_set("ID_KEY", client_applications(:oauth_web_app).key)
Object.const_set("POTLATCH2_KEY", client_applications(:oauth_web_app).key)
Object.const_set("ID_KEY", create(:client_application).key)
Object.const_set("POTLATCH2_KEY", create(:client_application).key)

stub_hostip_requests
end
Expand Down
6 changes: 6 additions & 0 deletions test/factories/client_applications.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FactoryGirl.define do
factory :client_application do
sequence(:name) { |n| "Client application #{n}" }
sequence(:url) { |n| "http://example.com/app/#{n}" }
end
end
50 changes: 0 additions & 50 deletions test/fixtures/client_applications.yml

This file was deleted.

2 changes: 1 addition & 1 deletion test/integration/client_applications_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "test_helper"

class ClientApplicationsTest < ActionDispatch::IntegrationTest
fixtures :users, :client_applications
fixtures :users

##
# run through the procedure of creating a client application and checking
Expand Down
16 changes: 8 additions & 8 deletions test/integration/oauth_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "test_helper"

class OAuthTest < ActionDispatch::IntegrationTest
fixtures :users, :client_applications, :gpx_files
fixtures :users, :gpx_files
set_fixture_class :gpx_files => Trace

include OAuth::Helper
Expand All @@ -11,18 +11,18 @@ def setup
end

def test_oauth10_web_app
client = client_applications(:oauth_web_app)
client = create(:client_application, :callback_url => "http://some.web.app.example.org/callback", :user => users(:public_user), :allow_read_prefs => true, :allow_write_api => true, :allow_read_gpx => true)

post_via_redirect "/login", :username => client.user.email, :password => "test"
assert_response :success

oauth10_without_callback(client)
oauth10_with_callback(client, "http://another.web.app.org/callback")
oauth10_with_callback(client, "http://another.web.app.example.org/callback")
oauth10_refused(client)
end

def test_oauth10_desktop_app
client = client_applications(:oauth_desktop_app)
client = create(:client_application, :user => users(:public_user), :allow_read_prefs => true, :allow_write_api => true, :allow_read_gpx => true)

post_via_redirect "/login", :username => client.user.email, :password => "test"
assert_response :success
Expand All @@ -32,18 +32,18 @@ def test_oauth10_desktop_app
end

def test_oauth10a_web_app
client = client_applications(:oauth_web_app)
client = create(:client_application, :callback_url => "http://some.web.app.example.org/callback", :user => users(:public_user), :allow_read_prefs => true, :allow_write_api => true, :allow_read_gpx => true)

post_via_redirect "/login", :username => client.user.email, :password => "test"
assert_response :success

oauth10a_without_callback(client)
oauth10a_with_callback(client, "http://another.web.app.org/callback")
oauth10a_with_callback(client, "http://another.web.app.example.org/callback")
oauth10a_refused(client)
end

def test_oauth10a_desktop_app
client = client_applications(:oauth_desktop_app)
client = create(:client_application, :user => users(:public_user), :allow_read_prefs => true, :allow_write_api => true, :allow_read_gpx => true)

post_via_redirect "/login", :username => client.user.email, :password => "test"
assert_response :success
Expand Down Expand Up @@ -196,7 +196,7 @@ def oauth10a_without_callback(client)
if client.callback_url
assert_response :redirect
verifier = parse_verifier(response)
assert_redirected_to "http://some.web.app.org/callback?oauth_token=#{token.token}&oauth_verifier=#{verifier}"
assert_redirected_to "http://some.web.app.example.org/callback?oauth_token=#{token.token}&oauth_verifier=#{verifier}"
else
assert_response :success
assert_template :authorize_success
Expand Down
14 changes: 6 additions & 8 deletions test/models/client_application_test.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
require "test_helper"

class ClientApplicationTest < ActiveSupport::TestCase
fixtures :client_applications

def test_url_valid
ok = ["http://example.com/test", "https://example.com/test"]
bad = ["", "ftp://example.com/test", "myapp://somewhere"]

ok.each do |url|
app = client_applications(:normal_user_app).dup
app = build(:client_application)
app.url = url
assert app.valid?, "#{url} is invalid, when it should be"
end

bad.each do |url|
app = client_applications(:normal_user_app)
app = build(:client_application)
app.url = url
assert !app.valid?, "#{url} is valid when it shouldn't be"
end
Expand All @@ -25,13 +23,13 @@ def test_support_url_valid
bad = ["ftp://example.com/test", "myapp://somewhere", "gibberish"]

ok.each do |url|
app = client_applications(:normal_user_app)
app = build(:client_application)
app.support_url = url
assert app.valid?, "#{url} is invalid, when it should be"
end

bad.each do |url|
app = client_applications(:normal_user_app)
app = build(:client_application)
app.support_url = url
assert !app.valid?, "#{url} is valid when it shouldn't be"
end
Expand All @@ -42,13 +40,13 @@ def test_callback_url_valid
bad = ["gibberish"]

ok.each do |url|
app = client_applications(:normal_user_app)
app = build(:client_application)
app.callback_url = url
assert app.valid?, "#{url} is invalid, when it should be"
end

bad.each do |url|
app = client_applications(:normal_user_app)
app = build(:client_application)
app.callback_url = url
assert !app.valid?, "#{url} is valid when it shouldn't be"
end
Expand Down
2 changes: 1 addition & 1 deletion test/models/oauth_token_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def test_token_invalidation
##
# check that an authorized token is authorised and can be invalidated
def test_token_authorisation
tok = RequestToken.create(:client_application => client_applications(:oauth_web_app))
tok = RequestToken.create(:client_application => create(:client_application))
assert_equal false, tok.authorized?, "Token should be created unauthorised."
tok.authorize!(users(:public_user))
assert_equal true, tok.authorized?, "Token should now be authorised."
Expand Down
2 changes: 0 additions & 2 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ def self.api_fixtures
set_fixture_class :gps_points => Tracepoint
set_fixture_class :gpx_file_tags => Tracetag

fixtures :client_applications

fixtures :redactions
end

Expand Down

0 comments on commit 8a6e8ae

Please sign in to comment.