2024-02-25 12:15:55 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "test_helper"
|
2014-07-23 18:15:47 -04:00
|
|
|
|
|
|
|
class ApiKeyTest < ActiveSupport::TestCase
|
|
|
|
context "in all cases a user" do
|
|
|
|
setup do
|
2022-11-25 15:06:54 -05:00
|
|
|
@user = create(:privileged_user, name: "abcdef")
|
2014-07-23 18:15:47 -04:00
|
|
|
@api_key = ApiKey.generate!(@user)
|
|
|
|
end
|
|
|
|
|
2018-05-15 17:19:45 -04:00
|
|
|
should "regenerate the key" do
|
|
|
|
assert_changes(-> { @api_key.key }) do
|
|
|
|
@api_key.regenerate!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
should "generate a unique key" do
|
|
|
|
assert_not_nil(@api_key.key)
|
|
|
|
end
|
|
|
|
|
2014-07-23 18:15:47 -04:00
|
|
|
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
|
2017-05-04 13:57:20 -04:00
|
|
|
|
|
|
|
should "have the same limits whether or not they have an api key" do
|
|
|
|
assert_no_difference(["@user.reload.api_regen_multiplier", "@user.reload.api_burst_limit"]) do
|
|
|
|
@user.api_key.destroy
|
|
|
|
end
|
|
|
|
end
|
2014-07-23 18:15:47 -04:00
|
|
|
end
|
|
|
|
end
|