Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 0 additions & 22 deletions lib/users_map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,3 @@ def index
class ::DirectoryItemsController
prepend DirectoryItemsControllerExtension
end

module UsersControllerLocationsExtension
def modify_user_params(attrs)
super(attrs)

if attrs &&
attrs[:custom_fields] &&
attrs[:custom_fields][:geo_location] &&
attrs[:custom_fields][:geo_location] != "{}" &&
(!attrs[:custom_fields][:geo_location]['lat'] ||
!attrs[:custom_fields][:geo_location]['lon'])
raise Discourse::InvalidParameters.new, I18n.t('location.errors.invalid')
end

attrs
end
end

require_dependency 'users_controller'
class ::UsersController
prepend UsersControllerLocationsExtension
end
17 changes: 16 additions & 1 deletion plugin.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true
# name: discourse-locations
# about: Tools for handling locations in Discourse
# version: 6.3.9
# version: 6.3.10
# authors: Angus McLeod, Robert Barrow
# contact_emails: development@pavilion.tech
# url: https://github.com/angusmcleod/discourse-locations
Expand Down Expand Up @@ -174,6 +174,21 @@ class Engine < ::Rails::Engine
load File.expand_path('../lib/users_map.rb', __FILE__)
load File.expand_path('../controllers/geocode.rb', __FILE__)

# check latitude and longitude are included when updating users location or raise and error
register_modifier(:users_controller_update_user_params) do |result, _, params|
if params &&
params[:custom_fields] &&
params[:custom_fields][:geo_location] &&
params[:custom_fields][:geo_location] != "{}" &&
(!params[:custom_fields][:geo_location]['lat'] ||
!params[:custom_fields][:geo_location]['lon'])
raise Discourse::InvalidParameters.new, I18n.t('location.errors.invalid')
end

result[:custom_fields][:geo_location] = params[:custom_fields][:geo_location]
result
end

unless Rails.env.test?
begin
Locations::Geocode.set_config
Expand Down
41 changes: 41 additions & 0 deletions spec/requests/user_controller_location_update_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: true
require 'rails_helper'

RSpec.describe UsersController do
fab!(:user) { Fabricate(:user) }
before do
sign_in(user)
SiteSetting.location_enabled = true
SiteSetting.location_users_map = true
end

context "locations plugin checks for valid geolocation parameters which at minimum need to include both latitude and longitude" do
it "allows user to upload valid geolocation to their profile" do
put "/u/#{user.username}.json",
params: {
custom_fields: {
geo_location: { lat: 10, lon: 12 },
},
}

expect(response.status).to eq(200)
result = response.parsed_body
expect(result["success"]).to eq("OK")
expect(user.custom_fields["geo_location"]["lat"]).to eq("10")
expect(user.custom_fields["geo_location"]["lon"]).to eq("12")
end

it "doesn't allow user to upload invalid geolocation to their profile" do
put "/u/#{user.username}.json",
params: {
custom_fields: {
geo_location: { lat: 10 },
},
}

expect(response.status).to eq(400)
result = response.parsed_body
expect(result["error_type"]).to eq("invalid_parameters")
end
end
end