This commit is contained in:
r888888888
2014-07-23 15:15:47 -07:00
parent 44b59ab18d
commit 6772566665
12 changed files with 207 additions and 15 deletions

View File

@@ -0,0 +1,36 @@
require 'test_helper'
class ApiKeysControllerTest < ActionController::TestCase
context "An api keys controller" do
setup do
@user = FactoryGirl.create(:gold_user)
end
context "#new" do
should "render" do
get :new, {}, {:user_id => @user.id}
assert_response :success
end
end
context "#create" do
should "succeed" do
assert_difference("ApiKey.count", 1) do
post :create, {}, {:user_id => @user.id}
end
end
context "when an api key already exists" do
setup do
ApiKey.generate!(@user)
end
should "not create another api key" do
assert_difference("ApiKey.count", 0) do
post :create, {}, {:user_id => @user.id}
end
end
end
end
end
end

View File

@@ -19,15 +19,16 @@ class PostsControllerTest < ActionController::TestCase
context "passing the api limit" do
setup do
User.any_instance.stubs(:api_hourly_limit).returns(5)
ApiKey.generate!(@user)
end
should "work" do
CurrentUser.user.api_hourly_limit.times do
get :index, {:format => "json", :login => @user.name, :api_key => @user.bcrypt_cookie_password_hash}
get :index, {:format => "json", :login => @user.name, :api_key => @user.api_key.key}
assert_response :success
end
get :index, {:format => "json", :login => @user.name, :api_key => @user.bcrypt_cookie_password_hash}
get :index, {:format => "json", :login => @user.name, :api_key => @user.api_key.key}
assert_response 421
end
end

23
test/unit/api_key_test.rb Normal file
View File

@@ -0,0 +1,23 @@
require 'test_helper'
class ApiKeyTest < ActiveSupport::TestCase
context "in all cases a user" do
setup do
@user = FactoryGirl.create(:user, :name => "abcdef")
@api_key = ApiKey.generate!(@user)
@user.name.mb_chars.downcase
end
should "authenticate via api key" do
assert_not_nil(User.authenticate_api_key(@user.name, @api_key.key))
end
should "not authenticate with the wrong api key" do
assert_nil(User.authenticate_api_key(@user.name, "xxx"))
end
should "not authenticate with the wrong name" do
assert_nil(User.authenticate_api_key("xxx", @api_key.key))
end
end
end