summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/action_rate_limiter_spec.rb
blob: 542fc03e55558e475f474cb4dc760fbe57e76840 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
require 'spec_helper'

describe Gitlab::ActionRateLimiter do
  let(:redis) { double('redis') }
  let(:user) { create(:user) }
  let(:project) { create(:project) }
  let(:key) { [user, project] }
  let(:cache_key) { "action_rate_limiter:test_action:user:#{user.id}:project:#{project.id}" }

  subject { described_class.new(action: :test_action, expiry_time: 100) }

  before do
    allow(Gitlab::Redis::Cache).to receive(:with).and_yield(redis)
  end

  it 'increases the throttle count and sets the expire time' do
    expect(redis).to receive(:incr).with(cache_key).and_return(1)
    expect(redis).to receive(:expire).with(cache_key, 100)

    expect(subject.throttled?(key, 1)).to be false
  end

  it 'returns true if the key is throttled' do
    expect(redis).to receive(:incr).with(cache_key).and_return(2)
    expect(redis).not_to receive(:expire)

    expect(subject.throttled?(key, 1)).to be true
  end
end