diff --git a/app/api/authenticated/profile.rb b/app/api/authenticated/profile.rb index 4e386cf..ae5a54b 100644 --- a/app/api/authenticated/profile.rb +++ b/app/api/authenticated/profile.rb @@ -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]) diff --git a/app/api/base.rb b/app/api/base.rb index 98d38e5..58d66b5 100644 --- a/app/api/base.rb +++ b/app/api/base.rb @@ -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? diff --git a/app/persistence/repository/account.rb b/app/persistence/repository/account.rb index 656b091..4d8f8fe 100644 --- a/app/persistence/repository/account.rb +++ b/app/persistence/repository/account.rb @@ -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) @@ -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] diff --git a/test/app/api/authenticated/profile_test.rb b/test/app/api/authenticated/profile_test.rb index ad6b782..9430829 100644 --- a/test/app/api/authenticated/profile_test.rb +++ b/test/app/api/authenticated/profile_test.rb @@ -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 diff --git a/test/webfixtures/photon.meier_single_result.json b/test/webfixtures/photon.meier_single_result.json new file mode 100644 index 0000000..bb03aea --- /dev/null +++ b/test/webfixtures/photon.meier_single_result.json @@ -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"}