Skip to content

Commit 296dd5f

Browse files
committed
Add DeleteApiKey command
1 parent 0c3c941 commit 296dd5f

File tree

3 files changed

+122
-25
lines changed

3 files changed

+122
-25
lines changed

Gemfile.lock

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ GEM
1616
ffi-compiler (~> 1.0)
1717
ast (2.4.3)
1818
base64 (0.3.0)
19-
bigdecimal (3.2.2)
19+
bigdecimal (3.2.3)
2020
byebug (12.0.0)
2121
coderay (1.1.3)
2222
crack (1.0.0)
@@ -64,14 +64,14 @@ GEM
6464
foobara-type-generator (< 2.0.0)
6565
foobara-typescript-react-command-form-generator (< 2.0.0)
6666
foobara-typescript-remote-command-generator (< 2.0.0)
67-
foobara (0.1.1)
67+
foobara (0.1.14)
6868
bigdecimal
6969
foobara-lru-cache (< 2.0.0)
7070
foobara-util (< 2.0.0)
7171
inheritable-thread-vars (< 2.0.0)
72-
foobara-command-generator (0.0.3)
73-
foobara
74-
foobara-files-generator
72+
foobara-command-generator (0.1.0)
73+
foobara (>= 0.1.7, < 2.0.0)
74+
foobara-files-generator (< 2.0.0)
7575
foobara-domain-generator (0.0.3)
7676
foobara
7777
foobara-files-generator
@@ -80,7 +80,7 @@ GEM
8080
foobara-files-generator
8181
foobara-dotenv-loader (0.0.3)
8282
dotenv
83-
foobara-empty-ruby-project-generator (1.0.1)
83+
foobara-empty-ruby-project-generator (1.0.4)
8484
extract-repo (< 2.0.0)
8585
foobara (>= 0.1.1, < 2.0.0)
8686
foobara-files-generator (< 2.0.0)
@@ -113,7 +113,7 @@ GEM
113113
foobara-resque-scheduler-connector-generator (0.0.2)
114114
foobara
115115
foobara-files-generator
116-
foobara-rubocop-rules (1.0.1)
116+
foobara-rubocop-rules (1.0.3)
117117
rubocop
118118
rubocop-rspec
119119
foobara-sh-cli-connector (1.1.0)
@@ -129,11 +129,11 @@ GEM
129129
foobara-typescript-react-command-form-generator (1.1.0)
130130
foobara (>= 0.1.1, < 2.0.0)
131131
foobara-typescript-remote-command-generator (< 2.0.0)
132-
foobara-typescript-remote-command-generator (1.1.0)
133-
foobara (>= 0.1.1, < 2.0.0)
132+
foobara-typescript-remote-command-generator (1.1.2)
133+
foobara (>= 0.1.14, < 2.0.0)
134134
foobara-files-generator (< 2.0.0)
135-
foobara-util (1.0.2)
136-
formatador (1.2.0)
135+
foobara-util (1.0.3)
136+
formatador (1.2.1)
137137
reline
138138
guard (2.19.1)
139139
formatador (>= 0.2.4)
@@ -151,14 +151,14 @@ GEM
151151
guard (~> 2.1)
152152
guard-compat (~> 1.1)
153153
rspec (>= 2.99.0, < 4.0)
154-
hashdiff (1.2.0)
154+
hashdiff (1.2.1)
155155
inheritable-thread-vars (0.0.3)
156156
io-console (0.8.1)
157157
irb (1.15.2)
158158
pp (>= 0.6.0)
159159
rdoc (>= 4.0.0)
160160
reline (>= 0.4.2)
161-
json (2.13.2)
161+
json (2.15.0)
162162
jwt (3.1.2)
163163
base64
164164
language_server-protocol (3.17.0.5)
@@ -167,7 +167,7 @@ GEM
167167
rb-fsevent (~> 0.10, >= 0.10.3)
168168
rb-inotify (~> 0.9, >= 0.9.10)
169169
logger (1.7.0)
170-
lumberjack (1.4.0)
170+
lumberjack (1.4.2)
171171
method_source (1.1.0)
172172
nenv (0.3.0)
173173
notiffany (0.1.3)
@@ -181,7 +181,7 @@ GEM
181181
pp (0.6.2)
182182
prettyprint
183183
prettyprint (0.2.0)
184-
prism (1.4.0)
184+
prism (1.5.1)
185185
pry (0.15.2)
186186
coderay (~> 1.1)
187187
method_source (~> 1.0)
@@ -201,10 +201,10 @@ GEM
201201
rdoc (6.14.2)
202202
erb
203203
psych (>= 4.0.0)
204-
regexp_parser (2.11.2)
204+
regexp_parser (2.11.3)
205205
reline (0.6.2)
206206
io-console (~> 0.5)
207-
rexml (3.4.1)
207+
rexml (3.4.4)
208208
rspec (3.13.1)
209209
rspec-core (~> 3.13.0)
210210
rspec-expectations (~> 3.13.0)
@@ -220,25 +220,25 @@ GEM
220220
rspec-mocks (3.13.5)
221221
diff-lcs (>= 1.2.0, < 2.0)
222222
rspec-support (~> 3.13.0)
223-
rspec-support (3.13.5)
224-
rubocop (1.80.0)
223+
rspec-support (3.13.6)
224+
rubocop (1.81.1)
225225
json (~> 2.3)
226226
language_server-protocol (~> 3.17.0.2)
227227
lint_roller (~> 1.1.0)
228228
parallel (~> 1.10)
229229
parser (>= 3.3.0.2)
230230
rainbow (>= 2.2.2, < 4.0)
231231
regexp_parser (>= 2.9.3, < 3.0)
232-
rubocop-ast (>= 1.46.0, < 2.0)
232+
rubocop-ast (>= 1.47.1, < 2.0)
233233
ruby-progressbar (~> 1.7)
234234
unicode-display_width (>= 2.4.0, < 4.0)
235-
rubocop-ast (1.46.0)
235+
rubocop-ast (1.47.1)
236236
parser (>= 3.3.7.2)
237237
prism (~> 1.4)
238238
rubocop-rake (0.7.1)
239239
lint_roller (~> 1.1)
240240
rubocop (>= 1.72.1)
241-
rubocop-rspec (3.6.0)
241+
rubocop-rspec (3.7.0)
242242
lint_roller (~> 1.1)
243243
rubocop (~> 1.72, >= 1.72.1)
244244
ruby-prof (1.7.2)
@@ -254,9 +254,9 @@ GEM
254254
stringio (3.1.7)
255255
thor (1.4.0)
256256
timecop (0.9.10)
257-
unicode-display_width (3.1.5)
258-
unicode-emoji (~> 4.0, >= 4.0.4)
259-
unicode-emoji (4.0.4)
257+
unicode-display_width (3.2.0)
258+
unicode-emoji (~> 4.1)
259+
unicode-emoji (4.1.0)
260260
vcr (6.3.1)
261261
base64
262262
webmock (3.25.1)

spec/delete_api_key_spec.rb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
RSpec.describe Foobara::Auth::DeleteApiKey do
2+
after { Foobara.reset_alls }
3+
4+
before do
5+
Foobara::Persistence.default_crud_driver = Foobara::Persistence::CrudDrivers::InMemory.new
6+
end
7+
8+
let(:command) { described_class.new(inputs) }
9+
let(:outcome) { command.run }
10+
let(:result) { outcome.result }
11+
let(:errors) { outcome.errors }
12+
let(:errors_hash) { outcome.errors_hash }
13+
14+
let(:inputs) do
15+
{ token: token_id_to_delete }
16+
end
17+
18+
let(:user) { Foobara::Auth::CreateUser.run!(username: "Basil", email: "[email protected]") }
19+
20+
context "when there are api keys" do
21+
let(:api_keys) do
22+
(0..4).map do
23+
Foobara::Auth::CreateApiKey.run!(user:)
24+
end
25+
end
26+
let(:api_key_summaries) do
27+
Foobara::Auth::GetApiKeySummaries.run!(user:)
28+
end
29+
let(:token_id_to_delete) { api_key_summaries[2].token_id }
30+
31+
def token_count
32+
Foobara::Auth::Types::Token.transaction do
33+
Foobara::Auth::Types::Token.count
34+
end
35+
end
36+
37+
it "deletes the api key from the user" do
38+
api_keys
39+
40+
expect(Foobara::Auth::GetApiKeySummaries.run!(user:).size).to be(5)
41+
expect(Foobara::Auth::FindUser.run!(id: user.id).api_keys.map(&:id)).to include(token_id_to_delete)
42+
43+
expect {
44+
expect(outcome).to be_success
45+
}.to change { token_count }.by(-1)
46+
47+
expect(Foobara::Auth::FindUser.run!(id: user.id).api_keys.map(&:id)).to_not include(token_id_to_delete)
48+
expect(Foobara::Auth::GetApiKeySummaries.run!(user:).size).to be(4)
49+
end
50+
end
51+
end

src/delete_api_key.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
require "securerandom"
2+
require "base64"
3+
4+
require_relative "build_secret"
5+
require_relative "create_token"
6+
7+
module Foobara
8+
module Auth
9+
class DeleteApiKey < Foobara::Command
10+
inputs do
11+
token Types::Token, :required
12+
end
13+
14+
def execute
15+
remove_api_key_from_user
16+
delete_token
17+
18+
nil
19+
end
20+
21+
def load_records
22+
super
23+
self.user = Types::User.that_owns(token, :api_keys)
24+
end
25+
26+
attr_accessor :user
27+
28+
# should Foobara do this automatically? Or at least support it? Similar to CASCADE in SQL
29+
def remove_api_key_from_user
30+
api_keys = user.api_keys
31+
32+
if api_keys.include?(token)
33+
user.api_keys = api_keys - [token]
34+
else
35+
# :nocov:
36+
raise "User has no such api key"
37+
# :nocov:
38+
end
39+
end
40+
41+
def delete_token
42+
token.hard_delete!
43+
end
44+
end
45+
end
46+
end

0 commit comments

Comments
 (0)