Skip to content

Commit

Permalink
Add test changing user's location
Browse files Browse the repository at this point in the history
  • Loading branch information
renatolond committed Nov 14, 2024
1 parent ecea9fb commit 677dff3
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 4 deletions.
2 changes: 1 addition & 1 deletion app/api/authenticated/profile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class Profile < Grape::API
results = PhotonClient.search(query: params[:location])
error!({ error: :UNEXPECTED_RESULTS_SIZE, detail: "Expected to have exactly one location with the given name, had #{results.size} instead" }, :unprocessable_content) if results.size != 1

Persistence::Repository::Account.update_profile_info(account_id: rodauth.session[:account_id], location: params["location"])
Persistence::Repository::Account.update_profile_location(account_id: rodauth.session[:account_id], location_result: results.first)
profile_info = Persistence::Repository::Account.profile_info(account_id: rodauth.session[:account_id])
status :ok
Entities::ProfileInfo.represent(profile_info, only: %i[location])
Expand Down
6 changes: 4 additions & 2 deletions app/api/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ class Base < Grape::API

helpers API::Helpers::Params

rescue_from :all do |_e|
error!({ error: "Internal server error" }, 500)
unless Environment.test?
rescue_from :all do |_e|
error!({ error: "Internal server error" }, 500)
end
end

if Environment.development?
Expand Down
11 changes: 10 additions & 1 deletion app/persistence/repository/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def basic_profile_info(account_id:)

# Updates the profile information for a given account
# Does not validate argument names passed to +args+, so if not validated before-hand can raise an exception
# @param account_id (see #profile_info)
# @param account_id (see .profile_info)
# @param args [Hash{Symbol => Object}] A hash containing the fields to be updated. Will not be verified for validity.
# @return [void]
def update_profile_info(account_id:, **args)
Expand All @@ -120,6 +120,15 @@ def update_profile_info(account_id:, **args)
account_informations.where(account_id:).update(args)
end

# Updates the profile location for a given account
# @param account_id (see .profile_info)
# @param location_result (see Persistence::Repository::Location.upsert_location)
# @return [void]
def update_profile_location(account_id:, location_result:)
location_id = Persistence::Repository::Location.upsert_location(location_result:)
account_informations.where(account_id:).update(location_id:)
end

private

# @return [Sequel::Postgres::Dataset]
Expand Down
15 changes: 15 additions & 0 deletions test/app/api/authenticated/profile_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,21 @@

assert_predicate last_response, :unprocessable?
end

it "sends a location that has exactly one result and updates the location for the user" do
body = {
location: "Méier, Rio de Janeiro, Região Metropolitana do Rio de Janeiro, Brazil"
}

stub_request(:get, "https://photon.komoot.io/api?q=M%C3%A9ier%2C+Rio+de+Janeiro%2C+Regi%C3%A3o+Metropolitana+do+Rio+de+Janeiro%2C+Brazil&layer=state&layer=county&layer=city&layer=district&limit=10&lang=en")
.to_return(webfixture_json_file("photon.meier_single_result"))

assert_difference "Location.count", 1 do
authorized_post @auth, @endpoint, body.to_json
end

assert_predicate last_response, :ok?
end
end

describe "post /profile/complete" do
Expand Down
12 changes: 12 additions & 0 deletions test/webfixtures/photon.meier_single_result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
HTTP/1.1 200 OK
Server: nginx/1.22.1
Date: Thu, 14 Nov 2024 11:20:44 GMT
Content-Type: application/json;charset=utf-8
Connection: keep-alive
Access-Control-Allow-Origin: *
Access-Control-Request-Method: get
Access-Control-Allow-Headers: *
Expires: Thu, 14 Nov 2024 12:20:44 GMT
Cache-Control: max-age=3600

{"features":[{"geometry":{"coordinates":[-43.2797093,-22.90173],"type":"Point"},"type":"Feature","properties":{"osm_type":"R","osm_id":5520336,"extent":[-43.2937516,-22.8921121,-43.2707757,-22.9106837],"country":"Brazil","osm_key":"place","city":"Rio de Janeiro","countrycode":"BR","osm_value":"suburb","name":"Méier","county":"Região Metropolitana do Rio de Janeiro","state":"Rio de Janeiro","type":"district"}}],"type":"FeatureCollection"}

0 comments on commit 677dff3

Please sign in to comment.