# But writing oauth tests is hard, and so it's easier to put in a controller test.)
def test_api_write_and_terms_agreed_via_token
user = create(:user, :terms_agreed => nil)
- token = create(:oauth_access_token, :resource_owner_id => user.id, :scopes => %w[write_api])
+ auth_header = bearer_authorization_header(user, :scopes => %w[write_api])
changeset = create(:changeset, :closed)
assert_difference "ChangesetComment.count", 0 do
- post changeset_comment_path(changeset), :params => { :text => "This is a comment" }, :headers => bearer_authorization_header(token.token)
+ post changeset_comment_path(changeset), :params => { :text => "This is a comment" }, :headers => auth_header
end
assert_response :forbidden
user.save!
assert_difference "ChangesetComment.count", 1 do
- post changeset_comment_path(changeset), :params => { :text => "This is a comment" }, :headers => bearer_authorization_header(token.token)
+ post changeset_comment_path(changeset), :params => { :text => "This is a comment" }, :headers => auth_header
end
assert_response :success
end
recipient = create(:user)
sender = create(:user)
- sender_token = create(:oauth_access_token,
- :resource_owner_id => sender.id,
- :scopes => %w[send_messages consume_messages])
- sender_auth = bearer_authorization_header(sender_token.token)
+ sender_auth = bearer_authorization_header(sender, :scopes => %w[send_messages consume_messages])
msg = build(:message)
recipient = create(:user)
sender = create(:user)
- sender_token = create(:oauth_access_token,
- :resource_owner_id => sender.id,
- :scopes => %w[send_messages consume_messages])
- sender_auth = bearer_authorization_header(sender_token.token)
+ sender_auth = bearer_authorization_header(sender, :scopes => %w[send_messages consume_messages])
assert_no_difference "Message.count" do
assert_no_difference "ActionMailer::Base.deliveries.size" do
sender = create(:user)
user3 = create(:user)
- sender_token = create(:oauth_access_token,
- :resource_owner_id => sender.id,
- :scopes => %w[consume_messages])
- sender_auth = bearer_authorization_header(sender_token.token)
-
- recipient_token = create(:oauth_access_token,
- :resource_owner_id => recipient.id,
- :scopes => %w[consume_messages])
- recipient_auth = bearer_authorization_header(recipient_token.token)
-
- user3_token = create(:oauth_access_token,
- :resource_owner_id => user3.id,
- :scopes => %w[send_messages consume_messages])
- user3_auth = bearer_authorization_header(user3_token.token)
+ sender_auth = bearer_authorization_header(sender, :scopes => %w[consume_messages])
+ recipient_auth = bearer_authorization_header(recipient, :scopes => %w[consume_messages])
+ user3_auth = bearer_authorization_header(user3, :scopes => %w[send_messages consume_messages])
msg = create(:message, :unread, :sender => sender, :recipient => recipient)
sender = create(:user)
user3 = create(:user)
- recipient_token = create(:oauth_access_token,
- :resource_owner_id => recipient.id,
- :scopes => %w[consume_messages])
- recipient_auth = bearer_authorization_header(recipient_token.token)
-
- user3_token = create(:oauth_access_token,
- :resource_owner_id => user3.id,
- :scopes => %w[send_messages consume_messages])
- user3_auth = bearer_authorization_header(user3_token.token)
+ recipient_auth = bearer_authorization_header(recipient, :scopes => %w[consume_messages])
+ user3_auth = bearer_authorization_header(user3, :scopes => %w[send_messages consume_messages])
msg = create(:message, :unread, :sender => sender, :recipient => recipient)
def test_delete
recipient = create(:user)
- recipient_token = create(:oauth_access_token,
- :resource_owner_id => recipient.id,
- :scopes => %w[consume_messages])
- recipient_auth = bearer_authorization_header(recipient_token.token)
+ recipient_auth = bearer_authorization_header(recipient, :scopes => %w[consume_messages])
sender = create(:user)
- sender_token = create(:oauth_access_token,
- :resource_owner_id => sender.id,
- :scopes => %w[send_messages consume_messages])
- sender_auth = bearer_authorization_header(sender_token.token)
+ sender_auth = bearer_authorization_header(sender, :scopes => %w[send_messages consume_messages])
user3 = create(:user)
- user3_token = create(:oauth_access_token,
- :resource_owner_id => user3.id,
- :scopes => %w[send_messages consume_messages])
- user3_auth = bearer_authorization_header(user3_token.token)
+ user3_auth = bearer_authorization_header(user3, :scopes => %w[send_messages consume_messages])
msg = create(:message, :read, :sender => sender, :recipient => recipient)
def test_list_messages
user1 = create(:user)
- user1_token = create(:oauth_access_token,
- :resource_owner_id => user1.id,
- :scopes => %w[send_messages consume_messages])
- user1_auth = bearer_authorization_header(user1_token.token)
+ user1_auth = bearer_authorization_header(user1, :scopes => %w[send_messages consume_messages])
user2 = create(:user)
- user2_token = create(:oauth_access_token,
- :resource_owner_id => user2.id,
- :scopes => %w[send_messages consume_messages])
- user2_auth = bearer_authorization_header(user2_token.token)
+ user2_auth = bearer_authorization_header(user2, :scopes => %w[send_messages consume_messages])
user3 = create(:user)
- user3_token = create(:oauth_access_token,
- :resource_owner_id => user3.id,
- :scopes => %w[send_messages consume_messages])
- user3_auth = bearer_authorization_header(user3_token.token)
+ user3_auth = bearer_authorization_header(user3, :scopes => %w[send_messages consume_messages])
# create some messages between users
# user | inbox | outbox
def test_paged_list_messages_asc
recipient = create(:user)
- recipient_token = create(:oauth_access_token,
- :resource_owner_id => recipient.id,
- :scopes => %w[consume_messages])
- recipient_auth = bearer_authorization_header(recipient_token.token)
+ recipient_auth = bearer_authorization_header(recipient, :scopes => %w[consume_messages])
sender = create(:user)
def test_paged_list_messages_desc
recipient = create(:user)
- recipient_token = create(:oauth_access_token,
- :resource_owner_id => recipient.id,
- :scopes => %w[consume_messages])
- recipient_auth = bearer_authorization_header(recipient_token.token)
+ recipient_auth = bearer_authorization_header(recipient, :scopes => %w[consume_messages])
sender = create(:user)
end
def test_redact_node_by_regular_with_read_prefs_scope
- auth_header = create_bearer_auth_header(create(:user), %w[read_prefs])
+ auth_header = bearer_authorization_header(create(:user), :scopes => %w[read_prefs])
do_redact_redactable_node(auth_header)
assert_response :forbidden, "should need to be moderator to redact."
end
def test_redact_node_by_regular_with_write_api_scope
- auth_header = create_bearer_auth_header(create(:user), %w[write_api])
+ auth_header = bearer_authorization_header(create(:user), :scopes => %w[write_api])
do_redact_redactable_node(auth_header)
assert_response :forbidden, "should need to be moderator to redact."
end
def test_redact_node_by_regular_with_write_redactions_scope
- auth_header = create_bearer_auth_header(create(:user), %w[write_redactions])
+ auth_header = bearer_authorization_header(create(:user), :scopes => %w[write_redactions])
do_redact_redactable_node(auth_header)
assert_response :forbidden, "should need to be moderator to redact."
end
def test_redact_node_by_moderator_with_read_prefs_scope
- auth_header = create_bearer_auth_header(create(:moderator_user), %w[read_prefs])
+ auth_header = bearer_authorization_header(create(:moderator_user), :scopes => %w[read_prefs])
do_redact_redactable_node(auth_header)
assert_response :forbidden, "should need to have write_redactions scope to redact."
end
def test_redact_node_by_moderator_with_write_api_scope
- auth_header = create_bearer_auth_header(create(:moderator_user), %w[write_api])
+ auth_header = bearer_authorization_header(create(:moderator_user), :scopes => %w[write_api])
do_redact_redactable_node(auth_header)
assert_response :success, "should be OK to redact old version as moderator with write_api scope."
# assert_response :forbidden, "should need to have write_redactions scope to redact."
end
def test_redact_node_by_moderator_with_write_redactions_scope
- auth_header = create_bearer_auth_header(create(:moderator_user), %w[write_redactions])
+ auth_header = bearer_authorization_header(create(:moderator_user), :scopes => %w[write_redactions])
do_redact_redactable_node(auth_header)
assert_response :success, "should be OK to redact old version as moderator with write_redactions scope."
end
private
- def create_bearer_auth_header(user, scopes)
- token = create(:oauth_access_token,
- :resource_owner_id => user.id,
- :scopes => scopes)
- bearer_authorization_header(token.token)
- end
-
def do_redact_redactable_node(headers = {})
node = create(:node, :with_history, :version => 4)
node_v3 = node.old_nodes.find_by(:version => 3)
end
def test_redact_relation_by_regular_with_read_prefs_scope
- auth_header = create_bearer_auth_header(create(:user), %w[read_prefs])
+ auth_header = bearer_authorization_header(create(:user), :scopes => %w[read_prefs])
do_redact_redactable_relation(auth_header)
assert_response :forbidden, "should need to be moderator to redact."
end
def test_redact_relation_by_regular_with_write_api_scope
- auth_header = create_bearer_auth_header(create(:user), %w[write_api])
+ auth_header = bearer_authorization_header(create(:user), :scopes => %w[write_api])
do_redact_redactable_relation(auth_header)
assert_response :forbidden, "should need to be moderator to redact."
end
def test_redact_relation_by_regular_with_write_redactions_scope
- auth_header = create_bearer_auth_header(create(:user), %w[write_redactions])
+ auth_header = bearer_authorization_header(create(:user), :scopes => %w[write_redactions])
do_redact_redactable_relation(auth_header)
assert_response :forbidden, "should need to be moderator to redact."
end
def test_redact_relation_by_moderator_with_read_prefs_scope
- auth_header = create_bearer_auth_header(create(:moderator_user), %w[read_prefs])
+ auth_header = bearer_authorization_header(create(:moderator_user), :scopes => %w[read_prefs])
do_redact_redactable_relation(auth_header)
assert_response :forbidden, "should need to have write_redactions scope to redact."
end
def test_redact_relation_by_moderator_with_write_api_scope
- auth_header = create_bearer_auth_header(create(:moderator_user), %w[write_api])
+ auth_header = bearer_authorization_header(create(:moderator_user), :scopes => %w[write_api])
do_redact_redactable_relation(auth_header)
assert_response :success, "should be OK to redact old version as moderator with write_api scope."
# assert_response :forbidden, "should need to have write_redactions scope to redact."
end
def test_redact_relation_by_moderator_with_write_redactions_scope
- auth_header = create_bearer_auth_header(create(:moderator_user), %w[write_redactions])
+ auth_header = bearer_authorization_header(create(:moderator_user), :scopes => %w[write_redactions])
do_redact_redactable_relation(auth_header)
assert_response :success, "should be OK to redact old version as moderator with write_redactions scope."
end
end
end
- def create_bearer_auth_header(user, scopes)
- token = create(:oauth_access_token,
- :resource_owner_id => user.id,
- :scopes => scopes)
- bearer_authorization_header(token.token)
- end
-
def do_redact_redactable_relation(headers = {})
relation = create(:relation, :with_history, :version => 4)
relation_v3 = relation.old_relations.find_by(:version => 3)
end
def test_redact_way_by_regular_with_read_prefs_scope
- auth_header = create_bearer_auth_header(create(:user), %w[read_prefs])
+ auth_header = bearer_authorization_header(create(:user), :scopes => %w[read_prefs])
do_redact_redactable_way(auth_header)
assert_response :forbidden, "should need to be moderator to redact."
end
def test_redact_way_by_regular_with_write_api_scope
- auth_header = create_bearer_auth_header(create(:user), %w[write_api])
+ auth_header = bearer_authorization_header(create(:user), :scopes => %w[write_api])
do_redact_redactable_way(auth_header)
assert_response :forbidden, "should need to be moderator to redact."
end
def test_redact_way_by_regular_with_write_redactions_scope
- auth_header = create_bearer_auth_header(create(:user), %w[write_redactions])
+ auth_header = bearer_authorization_header(create(:user), :scopes => %w[write_redactions])
do_redact_redactable_way(auth_header)
assert_response :forbidden, "should need to be moderator to redact."
end
def test_redact_way_by_moderator_with_read_prefs_scope
- auth_header = create_bearer_auth_header(create(:moderator_user), %w[read_prefs])
+ auth_header = bearer_authorization_header(create(:moderator_user), :scopes => %w[read_prefs])
do_redact_redactable_way(auth_header)
assert_response :forbidden, "should need to have write_redactions scope to redact."
end
def test_redact_way_by_moderator_with_write_api_scope
- auth_header = create_bearer_auth_header(create(:moderator_user), %w[write_api])
+ auth_header = bearer_authorization_header(create(:moderator_user), :scopes => %w[write_api])
do_redact_redactable_way(auth_header)
assert_response :success, "should be OK to redact old version as moderator with write_api scope."
# assert_response :forbidden, "should need to have write_redactions scope to redact."
end
def test_redact_way_by_moderator_with_write_redactions_scope
- auth_header = create_bearer_auth_header(create(:moderator_user), %w[write_redactions])
+ auth_header = bearer_authorization_header(create(:moderator_user), :scopes => %w[write_redactions])
do_redact_redactable_way(auth_header)
assert_response :success, "should be OK to redact old version as moderator with write_redactions scope."
end
end
end
- def create_bearer_auth_header(user, scopes)
- token = create(:oauth_access_token,
- :resource_owner_id => user.id,
- :scopes => scopes)
- bearer_authorization_header(token.token)
- end
-
def do_redact_redactable_way(headers = {})
way = create(:way, :with_history, :version => 4)
way_v3 = way.old_ways.find_by(:version => 3)
def test_permissions_oauth2
user = create(:user)
- token = create(:oauth_access_token,
- :resource_owner_id => user.id,
- :scopes => %w[read_prefs write_api])
- get permissions_path, :headers => bearer_authorization_header(token.token)
+ auth_header = bearer_authorization_header(user, :scopes => %w[read_prefs write_api])
+ get permissions_path, :headers => auth_header
assert_response :success
assert_select "osm > permissions", :count => 1 do
assert_select "permission", :count => 2
# read preferences
def test_show_using_token
user = create(:user)
- token = create(:oauth_access_token, :resource_owner_id => user.id, :scopes => %w[read_prefs])
+ auth_header = bearer_authorization_header(user, :scopes => %w[read_prefs])
create(:user_preference, :user => user, :k => "key", :v => "value")
- get user_preference_path(:preference_key => "key"), :headers => bearer_authorization_header(token.token)
+ get user_preference_path(:preference_key => "key"), :headers => auth_header
assert_response :success
end
# by other methods.
def test_show_using_token_fail
user = create(:user)
- token = create(:oauth_access_token, :resource_owner_id => user.id)
+ auth_header = bearer_authorization_header(user, :scopes => %w[])
create(:user_preference, :user => user, :k => "key", :v => "value")
- get user_preference_path(:preference_key => "key"), :headers => bearer_authorization_header(token.token)
+ get user_preference_path(:preference_key => "key"), :headers => auth_header
assert_response :forbidden
end
end
user = create(:user,
:home_lat => 12.1, :home_lon => 23.4,
:languages => ["en"])
- good_token = create(:oauth_access_token,
- :resource_owner_id => user.id,
- :scopes => %w[read_prefs])
- bad_token = create(:oauth_access_token,
- :resource_owner_id => user.id,
- :scopes => %w[])
+ good_auth = bearer_authorization_header(user, :scopes => %w[read_prefs])
+ bad_auth = bearer_authorization_header(user, :scopes => %w[])
other_user = create(:user,
:home_lat => 12.1, :home_lon => 23.4,
:languages => ["en"])
# check that we can fetch our own details as XML with read_prefs
- get api_user_path(:id => user.id), :headers => bearer_authorization_header(good_token.token)
+ get api_user_path(:id => user.id), :headers => good_auth
assert_response :success
assert_equal "application/xml", response.media_type
check_xml_details(user, true, false)
# check that we can fetch a different user's details as XML with read_prefs
- get api_user_path(:id => other_user.id), :headers => bearer_authorization_header(good_token.token)
+ get api_user_path(:id => other_user.id), :headers => good_auth
assert_response :success
assert_equal "application/xml", response.media_type
check_xml_details(other_user, false, false)
# check that we can fetch our own details as XML without read_prefs
- get api_user_path(:id => user.id), :headers => bearer_authorization_header(bad_token.token)
+ get api_user_path(:id => user.id), :headers => bad_auth
assert_response :success
assert_equal "application/xml", response.media_type
check_xml_details(user, false, false)
# check that we can fetch our own details as JSON with read_prefs
- get api_user_path(:id => user.id, :format => "json"), :headers => bearer_authorization_header(good_token.token)
+ get api_user_path(:id => user.id, :format => "json"), :headers => good_auth
assert_response :success
assert_equal "application/json", response.media_type
check_json_details(js, user, true, false)
# check that we can fetch a different user's details as JSON with read_prefs
- get api_user_path(:id => other_user.id, :format => "json"), :headers => bearer_authorization_header(good_token.token)
+ get api_user_path(:id => other_user.id, :format => "json"), :headers => good_auth
assert_response :success
assert_equal "application/json", response.media_type
check_json_details(js, other_user, false, false)
# check that we can fetch our own details as JSON without read_prefs
- get api_user_path(:id => user.id, :format => "json"), :headers => bearer_authorization_header(bad_token.token)
+ get api_user_path(:id => user.id, :format => "json"), :headers => bad_auth
assert_response :success
assert_equal "application/json", response.media_type
user = create(:user,
:home_lat => 12.1, :home_lon => 23.4,
:languages => ["en"])
- good_token = create(:oauth_access_token,
- :resource_owner_id => user.id,
- :scopes => %w[read_prefs])
- bad_token = create(:oauth_access_token,
- :resource_owner_id => user.id)
- email_token = create(:oauth_access_token,
- :resource_owner_id => user.id,
- :scopes => %w[read_prefs read_email])
+ good_auth = bearer_authorization_header(user, :scopes => %w[read_prefs])
+ bad_auth = bearer_authorization_header(user, :scopes => %w[])
+ email_auth = bearer_authorization_header(user, :scopes => %w[read_prefs read_email])
# check that we can't fetch details as XML without read_prefs
- get user_details_path, :headers => bearer_authorization_header(bad_token.token)
+ get user_details_path, :headers => bad_auth
assert_response :forbidden
# check that we can fetch details as XML without read_email
- get user_details_path, :headers => bearer_authorization_header(good_token.token)
+ get user_details_path, :headers => good_auth
assert_response :success
assert_equal "application/xml", response.media_type
check_xml_details(user, true, false)
# check that we can fetch details as XML with read_email
- get user_details_path, :headers => bearer_authorization_header(email_token.token)
+ get user_details_path, :headers => email_auth
assert_response :success
assert_equal "application/xml", response.media_type
check_xml_details(user, true, true)
# check that we can't fetch details as JSON without read_prefs
- get user_details_path(:format => "json"), :headers => bearer_authorization_header(bad_token.token)
+ get user_details_path(:format => "json"), :headers => bad_auth
assert_response :forbidden
# check that we can fetch details as JSON without read_email
- get user_details_path(:format => "json"), :headers => bearer_authorization_header(good_token.token)
+ get user_details_path(:format => "json"), :headers => good_auth
assert_response :success
assert_equal "application/json", response.media_type
check_json_details(js, user, true, false)
# check that we can fetch details as JSON with read_email
- get user_details_path(:format => "json"), :headers => bearer_authorization_header(email_token.token)
+ get user_details_path(:format => "json"), :headers => email_auth
assert_response :success
assert_equal "application/json", response.media_type
user1 = create(:user, :description => "test1", :terms_agreed => Date.yesterday)
user2 = create(:user, :description => "test2", :terms_agreed => Date.yesterday)
user3 = create(:user, :description => "test3", :terms_agreed => Date.yesterday)
- good_token = create(:oauth_access_token, :resource_owner_id => user1.id, :scopes => %w[read_prefs])
- bad_token = create(:oauth_access_token, :resource_owner_id => user1.id, :scopes => %w[])
+ good_auth = bearer_authorization_header(user1, :scopes => %w[read_prefs])
+ bad_auth = bearer_authorization_header(user1, :scopes => %w[])
- get api_users_path, :params => { :users => user1.id }, :headers => bearer_authorization_header(good_token.token)
+ get api_users_path, :params => { :users => user1.id }, :headers => good_auth
assert_response :success
assert_equal "application/xml", response.media_type
assert_select "user", :count => 1 do
assert_select "user[id='#{user3.id}']", :count => 0
end
- get api_users_path, :params => { :users => user2.id }, :headers => bearer_authorization_header(good_token.token)
+ get api_users_path, :params => { :users => user2.id }, :headers => good_auth
assert_response :success
assert_equal "application/xml", response.media_type
assert_select "user", :count => 1 do
assert_select "user[id='#{user3.id}']", :count => 0
end
- get api_users_path, :params => { :users => "#{user1.id},#{user3.id}" }, :headers => bearer_authorization_header(good_token.token)
+ get api_users_path, :params => { :users => "#{user1.id},#{user3.id}" }, :headers => good_auth
assert_response :success
assert_equal "application/xml", response.media_type
assert_select "user", :count => 2 do
check_xml_details(user3, false, false)
end
- get api_users_path, :params => { :users => "#{user1.id},#{user3.id}" }, :headers => bearer_authorization_header(bad_token.token)
+ get api_users_path, :params => { :users => "#{user1.id},#{user3.id}" }, :headers => bad_auth
assert_response :success
assert_equal "application/xml", response.media_type
assert_select "user", :count => 2 do
check_xml_details(user3, false, false)
end
- get api_users_path, :params => { :users => user1.id, :format => "json" }, :headers => bearer_authorization_header(good_token.token)
+ get api_users_path, :params => { :users => user1.id, :format => "json" }, :headers => good_auth
assert_response :success
assert_equal "application/json", response.media_type
js = ActiveSupport::JSON.decode(@response.body)
assert_equal 1, js["users"].count
check_json_details(js["users"][0], user1, true, false)
- get api_users_path, :params => { :users => user2.id, :format => "json" }, :headers => bearer_authorization_header(good_token.token)
+ get api_users_path, :params => { :users => user2.id, :format => "json" }, :headers => good_auth
assert_response :success
assert_equal "application/json", response.media_type
js = ActiveSupport::JSON.decode(@response.body)
assert_equal 1, js["users"].count
check_json_details(js["users"][0], user2, false, false)
- get api_users_path, :params => { :users => "#{user1.id},#{user3.id}", :format => "json" }, :headers => bearer_authorization_header(good_token.token)
+ get api_users_path, :params => { :users => "#{user1.id},#{user3.id}", :format => "json" }, :headers => good_auth
assert_response :success
assert_equal "application/json", response.media_type
js = ActiveSupport::JSON.decode(@response.body)
check_json_details(js["users"][0], user1, true, false)
check_json_details(js["users"][1], user3, false, false)
- get api_users_path, :params => { :users => "#{user1.id},#{user3.id}", :format => "json" }, :headers => bearer_authorization_header(bad_token.token)
+ get api_users_path, :params => { :users => "#{user1.id},#{user3.id}", :format => "json" }, :headers => bad_auth
assert_response :success
assert_equal "application/json", response.media_type
js = ActiveSupport::JSON.decode(@response.body)
check_json_details(js["users"][0], user1, false, false)
check_json_details(js["users"][1], user3, false, false)
- get api_users_path, :params => { :users => create(:user, :suspended).id }, :headers => bearer_authorization_header(good_token.token)
+ get api_users_path, :params => { :users => create(:user, :suspended).id }, :headers => good_auth
assert_response :success
assert_equal "application/xml", response.media_type
assert_select "user", :count => 0
- get api_users_path, :params => { :users => create(:user, :deleted).id }, :headers => bearer_authorization_header(good_token.token)
+ get api_users_path, :params => { :users => create(:user, :deleted).id }, :headers => good_auth
assert_response :success
assert_equal "application/xml", response.media_type
assert_select "user", :count => 0
- get api_users_path, :params => { :users => 0 }, :headers => bearer_authorization_header(good_token.token)
+ get api_users_path, :params => { :users => 0 }, :headers => good_auth
assert_response :success
assert_equal "application/xml", response.media_type
assert_select "user", :count => 0