Skip to content

Commit

Permalink
chore: add some tests (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcmrf authored Oct 8, 2024
1 parent 6d913bc commit 9ae7974
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions spec/plugin_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# spec/plugin_spec.rb
# frozen_string_literal: true

RSpec.describe LastDayUsedKey do
let(:user) { Fabricate(:user) }
let(:user_api_key) { Fabricate(:user_api_key, user: user) }
let(:api_key) { Fabricate(:api_key, user: user) }

describe '#update_last_used for UserApiKey' do
it 'updates last_used_at to beginning of the day if not already set' do
user_api_key.update!(last_used_at: 2.days.ago)

expect {
user_api_key.update_last_used("client_id_1")
}.to change { user_api_key.reload.last_used_at }
.to(Time.zone.now.beginning_of_day)
end

it 'does not update last_used_at if already set to beginning of the day' do
user_api_key.update!(last_used_at: Time.zone.now.beginning_of_day, client_id: "client_id_1")

allow(user_api_key).to receive(:update_column)
allow(user_api_key).to receive(:update_columns)

expect {
user_api_key.update_last_used("client_id_1")
}.not_to change { user_api_key.reload.last_used_at }

expect(user_api_key).not_to have_received(:update_column)
expect(user_api_key).not_to have_received(:update_columns)
end

it 'updates client_id and destroys other keys with same client_id and user_id' do
Fabricate(:user_api_key, user: user, client_id: "client_id_1")

expect {
user_api_key.update_last_used("client_id_1")
}.to change { user_api_key.reload.client_id }.to("client_id_1")
expect(UserApiKey.where(client_id: "client_id_1", user_id: user.id).count).to eq(1)
end
end

describe '#update_last_used! for ApiKey' do
let(:api_key) { Fabricate(:api_key, last_used_at: nil) }

it 'updates last_used_at to the beginning of the day if not already set' do
expect {
api_key.update_last_used!
}.to change { api_key.reload.last_used_at }
.to(Time.zone.now.beginning_of_day)
end

it 'does not perform any database write if last_used_at is already set to the beginning of the day' do
api_key.update!(last_used_at: Time.zone.now.beginning_of_day)

allow(api_key).to receive(:update_column)
allow(api_key).to receive(:update_columns)

api_key.update_last_used!

expect(api_key).not_to have_received(:update_column)
expect(api_key).not_to have_received(:update_columns)
end
end
end

0 comments on commit 9ae7974

Please sign in to comment.