generated from discourse/discourse-plugin-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |