Skip to content

Commit

Permalink
Add index method to track/{track}/trophies API controller (#6944)
Browse files Browse the repository at this point in the history
* Add index method to track/{track}/trophies API controller

* Add tests

---------

Co-authored-by: Jeremy Walker <jez.walker@gmail.com>
  • Loading branch information
glennj and iHiD authored Jun 17, 2024
1 parent c90ea27 commit a29aacf
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
6 changes: 6 additions & 0 deletions app/controllers/api/tracks/trophies_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
class API::Tracks::TrophiesController < API::BaseController
def index
track = Track.find(params[:track_slug])

render json: SerializeTrackTrophies.(track, current_user)
end

def reveal
begin
track = Track.find(params[:track_slug])
Expand Down
2 changes: 1 addition & 1 deletion config/routes/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
resources :concepts, only: [], param: :slug do
resources :makers, only: [:index], controller: "concepts/makers"
end
resources :trophies, only: [], param: :uuid, controller: "tracks/trophies" do
resources :trophies, only: [:index], param: :uuid, controller: "tracks/trophies" do
member do
patch :reveal
end
Expand Down
30 changes: 29 additions & 1 deletion test/controllers/api/tracks/trophies_controller_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,35 @@
require_relative '../base_test_case'

class API::Tracks::TrophiesControllerTest < API::BaseTestCase
# guard_incorrect_token! :reveal_api_track_trophy_path, args: 2, method: :patch
guard_incorrect_token! :reveal_api_track_trophy_path, args: 2, method: :patch
guard_incorrect_token! :api_track_trophies_path, args: 1, method: :get

test "index with none revealed" do
track = create :track
create :trophy, valid_track_slugs: [track.slug]
create :functional_trophy, valid_track_slugs: [track.slug]

sign_in!
get api_track_trophies_url(track.slug), headers: @headers, as: :json

assert_response :ok
expected = SerializeTrackTrophies.(track, User.first).to_json
assert_equal expected, response.body
end

test "index with one revealed" do
user = create :user
track = create :track
trophy = create :trophy, valid_track_slugs: [track.slug]
create :user_track_acquired_trophy, user:, track:, trophy:, revealed: true

sign_in!(user)
get api_track_trophies_url(track.slug), headers: @headers, as: :json

assert_response :ok
expected = SerializeTrackTrophies.(track, user).to_json
assert_equal expected, response.body
end

##########
# Reveal #
Expand Down

0 comments on commit a29aacf

Please sign in to comment.