From ff441bc4caecd67627a199852ae53a5992bed456 Mon Sep 17 00:00:00 2001 From: Bianca Nenciu Date: Wed, 22 Aug 2018 23:19:01 +0200 Subject: [PATCH] Add spec for UserSerializer and UserApiKey. --- spec/serializers/user_serializer_spec.rb | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/spec/serializers/user_serializer_spec.rb b/spec/serializers/user_serializer_spec.rb index 6ed8891c646..a637d13137b 100644 --- a/spec/serializers/user_serializer_spec.rb +++ b/spec/serializers/user_serializer_spec.rb @@ -196,4 +196,25 @@ describe UserSerializer do expect(json[:custom_fields]['secret_field']).to eq(nil) end end + + context "with user_api_keys" do + let(:user) { Fabricate(:user) } + + it "sorts keys by last used time" do + freeze_time + + user_api_key_0 = Fabricate(:readonly_user_api_key, user: user, last_used_at: 2.days.ago, revoked_at: Time.zone.now) + user_api_key_1 = Fabricate(:readonly_user_api_key, user: user, last_used_at: 7.days.ago) + user_api_key_2 = Fabricate(:readonly_user_api_key, user: user, last_used_at: 1.days.ago) + user_api_key_3 = Fabricate(:readonly_user_api_key, user: user, last_used_at: 4.days.ago, revoked_at: Time.zone.now) + user_api_key_4 = Fabricate(:readonly_user_api_key, user: user, last_used_at: 3.days.ago) + + json = UserSerializer.new(user, scope: Guardian.new(user), root: false).as_json + + expect(json[:user_api_keys].size).to eq(3) + expect(json[:user_api_keys][0][:id]).to eq(user_api_key_1.id) + expect(json[:user_api_keys][1][:id]).to eq(user_api_key_4.id) + expect(json[:user_api_keys][2][:id]).to eq(user_api_key_2.id) + end + end end