Merge branch 'master' into skeb
This commit is contained in:
@@ -48,11 +48,5 @@ class ApiKeyTest < ActiveSupport::TestCase
|
||||
should "not authenticate with the wrong name" do
|
||||
assert_equal(false, create(:user).authenticate_api_key(@api_key.key))
|
||||
end
|
||||
|
||||
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
|
||||
@api_key.destroy
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -33,7 +33,7 @@ class IpGeolocationTest < ActiveSupport::TestCase
|
||||
should "work for a residential IP" do
|
||||
@ip = IpGeolocation.create_or_update!("2a01:0e35:2f22:e3d0::1")
|
||||
|
||||
assert_equal(26, @ip.network.prefix)
|
||||
assert_equal(28, @ip.network.prefix)
|
||||
assert_equal(false, @ip.is_proxy?)
|
||||
assert_equal(49, @ip.latitude.round(0))
|
||||
assert_equal(2, @ip.longitude.round(0))
|
||||
|
||||
72
test/unit/rate_limit_test.rb
Normal file
72
test/unit/rate_limit_test.rb
Normal file
@@ -0,0 +1,72 @@
|
||||
require 'test_helper'
|
||||
|
||||
class RateLimitTest < ActiveSupport::TestCase
|
||||
context "RateLimit: " do
|
||||
context "#limit! method" do
|
||||
should "create a new rate limit object if none exists, or update it if it already exists" do
|
||||
assert_difference("RateLimit.count", 1) do
|
||||
RateLimiter.new("write", ["users/1"]).limited?
|
||||
end
|
||||
|
||||
assert_difference("RateLimit.count", 0) do
|
||||
RateLimiter.new("write", ["users/1"]).limited?
|
||||
end
|
||||
|
||||
assert_difference("RateLimit.count", 1) do
|
||||
RateLimiter.new("write", ["users/1", "ip/1.2.3.4"]).limited?
|
||||
end
|
||||
|
||||
assert_difference("RateLimit.count", 0) do
|
||||
RateLimiter.new("write", ["users/1", "ip/1.2.3.4"]).limited?
|
||||
end
|
||||
end
|
||||
|
||||
should "include the cost of the first action when initializing the limit" do
|
||||
limiter = RateLimiter.new("write", ["users/1"], burst: 10, cost: 1)
|
||||
assert_equal(9, limiter.rate_limits.first.points)
|
||||
end
|
||||
|
||||
should "be limited and penalize the caller 1 second if the point count is negative" do
|
||||
freeze_time
|
||||
create(:rate_limit, action: "write", key: "users/1", points: -1)
|
||||
limiter = RateLimiter.new("write", ["users/1"], cost: 1)
|
||||
|
||||
assert_equal(true, limiter.limited?)
|
||||
assert_equal(-2, limiter.rate_limits.first.points)
|
||||
end
|
||||
|
||||
should "not be limited if the point count was positive before the action" do
|
||||
freeze_time
|
||||
create(:rate_limit, action: "write", key: "users/1", points: 0.01)
|
||||
limiter = RateLimiter.new("write", ["users/1"], cost: 1)
|
||||
|
||||
assert_equal(false, limiter.limited?)
|
||||
assert_equal(-0.99, limiter.rate_limits.first.points)
|
||||
end
|
||||
|
||||
should "refill the points at the correct rate" do
|
||||
freeze_time
|
||||
create(:rate_limit, action: "write", key: "users/1", points: -2)
|
||||
|
||||
limiter = RateLimiter.new("write", ["users/1"], cost: 1, rate: 1, burst: 10)
|
||||
assert_equal(true, limiter.limited?)
|
||||
assert_equal(-3, limiter.rate_limits.first.points)
|
||||
|
||||
travel 1.second
|
||||
limiter = RateLimiter.new("write", ["users/1"], cost: 1, rate: 1, burst: 10)
|
||||
assert_equal(true, limiter.limited?)
|
||||
assert_equal(-3, limiter.rate_limits.first.points)
|
||||
|
||||
travel 5.second
|
||||
limiter = RateLimiter.new("write", ["users/1"], cost: 1, rate: 1, burst: 10)
|
||||
assert_equal(false, limiter.limited?)
|
||||
assert_equal(1, limiter.rate_limits.first.points)
|
||||
|
||||
travel 60.second
|
||||
limiter = RateLimiter.new("write", ["users/1"], cost: 1, rate: 1, burst: 10)
|
||||
assert_equal(false, limiter.limited?)
|
||||
assert_equal(9, limiter.rate_limits.first.points)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -95,20 +95,30 @@ module Sources
|
||||
should "fetch the source data" do
|
||||
assert_equal("evazion", @site.artist_name)
|
||||
end
|
||||
|
||||
should "correctly get the page url" do
|
||||
assert_equal(@ref, @site.page_url)
|
||||
end
|
||||
end
|
||||
|
||||
context "A baraag url" do
|
||||
setup do
|
||||
skip "Baraag keys not set" unless Danbooru.config.baraag_client_id
|
||||
@url = "https://baraag.net/@bardbot/105732813175612920"
|
||||
@site = Sources::Strategies.find(@url)
|
||||
@site1 = Sources::Strategies.find(@url)
|
||||
|
||||
@img = "https://baraag.net/system/media_attachments/files/105/803/948/862/719/091/original/54e1cb7ca33ec449.png"
|
||||
@ref = "https://baraag.net/@Nakamura/105803949565505009"
|
||||
@site2 = Sources::Strategies.find(@img, @ref)
|
||||
end
|
||||
|
||||
should "work" do
|
||||
assert_equal("https://baraag.net/@bardbot", @site.profile_url)
|
||||
assert_equal(["https://baraag.net/system/media_attachments/files/105/732/803/241/495/700/original/556e1eb7f5ca610f.png"], @site.image_urls)
|
||||
assert_equal("bardbot", @site.artist_name)
|
||||
assert_equal("🍌", @site.dtext_artist_commentary_desc)
|
||||
assert_equal("https://baraag.net/@bardbot", @site1.profile_url)
|
||||
assert_equal(["https://baraag.net/system/media_attachments/files/105/732/803/241/495/700/original/556e1eb7f5ca610f.png"], @site1.image_urls)
|
||||
assert_equal("bardbot", @site1.artist_name)
|
||||
assert_equal("🍌", @site1.dtext_artist_commentary_desc)
|
||||
|
||||
assert_equal([@img], @site2.image_urls)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -305,6 +305,18 @@ module Sources
|
||||
end
|
||||
end
|
||||
|
||||
context "when the cached session cookie is invalid" do
|
||||
should "clear the cached cookie after failing to fetch the data" do
|
||||
site = Sources::Strategies.find("https://nijie.info/view.php?id=203688")
|
||||
|
||||
Cache.put("nijie-session-cookie", HTTP::Cookie.new(name: "NIJIEIJIEID", value: "fake", domain: "nijie.info", path: "/"))
|
||||
assert_equal("fake", site.cached_session_cookie.value)
|
||||
|
||||
assert_equal([], site.image_urls)
|
||||
assert_nil(Cache.get("nijie-session-cookie"))
|
||||
end
|
||||
end
|
||||
|
||||
context "a doujin post" do
|
||||
should "work" do
|
||||
image = "https://pic.nijie.net/01/dojin_main/dojin_sam/20120213044700%E3%82%B3%E3%83%94%E3%83%BC%20%EF%BD%9E%200011%E3%81%AE%E3%82%B3%E3%83%94%E3%83%BC.jpg"
|
||||
|
||||
@@ -1,45 +0,0 @@
|
||||
require 'test_helper'
|
||||
|
||||
class TokenBucketTest < ActiveSupport::TestCase
|
||||
context "#add!" do
|
||||
setup do
|
||||
@user = FactoryBot.create(:user)
|
||||
TokenBucket.create(user_id: @user.id, last_touched_at: 1.minute.ago, token_count: 0)
|
||||
end
|
||||
|
||||
should "work" do
|
||||
@user.token_bucket.add!
|
||||
assert_operator(@user.token_bucket.token_count, :>, 0)
|
||||
@user.reload
|
||||
assert_operator(@user.token_bucket.token_count, :>, 0)
|
||||
end
|
||||
end
|
||||
|
||||
context "#consume!" do
|
||||
setup do
|
||||
@user = FactoryBot.create(:user)
|
||||
TokenBucket.create(user_id: @user.id, last_touched_at: 1.minute.ago, token_count: 1)
|
||||
end
|
||||
|
||||
should "work" do
|
||||
@user.token_bucket.consume!
|
||||
assert_operator(@user.token_bucket.token_count, :<, 1)
|
||||
@user.reload
|
||||
assert_operator(@user.token_bucket.token_count, :<, 1)
|
||||
end
|
||||
end
|
||||
|
||||
context "#throttled?" do
|
||||
setup do
|
||||
@user = FactoryBot.create(:user)
|
||||
TokenBucket.create(user_id: @user.id, last_touched_at: 1.minute.ago, token_count: 0)
|
||||
end
|
||||
|
||||
should "work" do
|
||||
assert(!@user.token_bucket.throttled?)
|
||||
assert_operator(@user.token_bucket.token_count, :<, 60)
|
||||
@user.reload
|
||||
assert_operator(@user.token_bucket.token_count, :<, 60)
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user