Skip to content

Commit 3a2dc1d

Browse files
committed
Fix family location sharing toggle
1 parent d1ffc15 commit 3a2dc1d

File tree

7 files changed

+223
-30
lines changed

7 files changed

+223
-30
lines changed

app/assets/builds/tailwind.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# frozen_string_literal: true
2+
3+
class Api::V1::Families::LocationsController < ApiController
4+
before_action :ensure_family_feature_enabled!
5+
6+
# Skip API key auth for toggle action (use session auth instead for browser)
7+
skip_before_action :authenticate_api_key, only: [:toggle]
8+
before_action :authenticate_user!, only: [:toggle]
9+
before_action :ensure_user_in_family!
10+
11+
def index
12+
family_locations = Families::Locations.new(current_api_user).call
13+
14+
render json: {
15+
locations: family_locations,
16+
updated_at: Time.current.iso8601,
17+
sharing_enabled: current_api_user.family_sharing_enabled?
18+
}
19+
end
20+
21+
def toggle
22+
result = Families::UpdateLocationSharing.new(
23+
user: current_user,
24+
enabled: params[:enabled],
25+
duration: params[:duration]
26+
).call
27+
28+
render json: result.payload, status: result.status
29+
end
30+
31+
private
32+
33+
def ensure_user_in_family!
34+
user = action_name == 'toggle' ? current_user : current_api_user
35+
return if user&.in_family?
36+
37+
render json: { error: 'User is not part of a family' }, status: :forbidden
38+
end
39+
end

app/controllers/api/v1/families_controller.rb

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
11
# frozen_string_literal: true
22

3+
# This controller is kept for future family-level API endpoints
4+
# Location-specific endpoints have been moved to Api::V1::Families::LocationsController
35
class Api::V1::FamiliesController < ApiController
46
before_action :ensure_family_feature_enabled!
57
before_action :ensure_user_in_family!
68

7-
def locations
8-
family_locations = Families::Locations.new(current_api_user).call
9-
10-
render json: {
11-
locations: family_locations,
12-
updated_at: Time.current.iso8601,
13-
sharing_enabled: current_api_user.family_sharing_enabled?
14-
}
15-
end
16-
179
private
1810

1911
def ensure_user_in_family!

app/controllers/families_controller.rb

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
class FamiliesController < ApplicationController
44
before_action :authenticate_user!
55
before_action :ensure_family_feature_enabled!
6-
before_action :set_family, only: %i[show edit update destroy update_location_sharing]
6+
before_action :set_family, only: %i[show edit update destroy]
77

88
def show
99
authorize @family
@@ -76,18 +76,6 @@ def destroy
7676
end
7777
end
7878

79-
def update_location_sharing
80-
authorize @family, :update_location_sharing?
81-
82-
result = Families::UpdateLocationSharing.new(
83-
user: current_user,
84-
enabled: params[:enabled],
85-
duration: params[:duration]
86-
).call
87-
88-
render json: result.payload, status: result.status
89-
end
90-
9179
private
9280

9381
def set_family

app/javascript/controllers/location_sharing_toggle_controller.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export default class extends Controller {
6262
try {
6363
const csrfToken = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
6464

65-
const response = await fetch(`/family/update_location_sharing`, {
65+
const response = await fetch(`/api/v1/families/locations/toggle`, {
6666
method: 'PATCH',
6767
headers: {
6868
'Accept': 'application/json',

config/routes.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@
6060
# Family management routes (only if feature is enabled)
6161
if DawarichSettings.family_feature_enabled?
6262
resource :family, only: %i[show new create edit update destroy] do
63-
patch :update_location_sharing, on: :member
64-
6563
resources :invitations, except: %i[edit update], controller: 'family/invitations'
6664
resources :members, only: %i[destroy], controller: 'family/memberships'
6765
end
@@ -171,9 +169,11 @@
171169
end
172170
end
173171

174-
resources :families, only: [] do
175-
collection do
176-
get :locations
172+
namespace :families do
173+
resources :locations, only: [:index] do
174+
collection do
175+
patch :toggle
176+
end
177177
end
178178
end
179179

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails_helper'
4+
5+
RSpec.describe 'Api::V1::Families::Locations', type: :request do
6+
include ActiveSupport::Testing::TimeHelpers
7+
8+
let(:user) { create(:user) }
9+
let(:other_user) { create(:user) }
10+
let(:family) { create(:family, creator: user) }
11+
let!(:user_membership) { create(:family_membership, user: user, family: family, role: :owner) }
12+
13+
describe 'GET /api/v1/families/locations' do
14+
context 'with valid API key' do
15+
before do
16+
create(:family_membership, user: other_user, family: family, role: :member)
17+
other_user.update_family_location_sharing!(true, duration: 'permanent')
18+
end
19+
20+
it 'returns family member locations' do
21+
get '/api/v1/families/locations', params: { api_key: user.api_key }
22+
23+
expect(response).to have_http_status(:ok)
24+
json_response = JSON.parse(response.body)
25+
expect(json_response).to have_key('locations')
26+
expect(json_response).to have_key('updated_at')
27+
expect(json_response).to have_key('sharing_enabled')
28+
end
29+
30+
it 'includes sharing status' do
31+
user.update_family_location_sharing!(true, duration: 'permanent')
32+
33+
get '/api/v1/families/locations', params: { api_key: user.api_key }
34+
35+
json_response = JSON.parse(response.body)
36+
expect(json_response['sharing_enabled']).to be true
37+
end
38+
end
39+
40+
context 'without API key' do
41+
it 'returns unauthorized' do
42+
get '/api/v1/families/locations'
43+
44+
expect(response).to have_http_status(:unauthorized)
45+
end
46+
end
47+
48+
context 'with invalid API key' do
49+
it 'returns unauthorized' do
50+
get '/api/v1/families/locations', params: { api_key: 'invalid' }
51+
52+
expect(response).to have_http_status(:unauthorized)
53+
end
54+
end
55+
56+
context 'when user is not in a family' do
57+
let(:solo_user) { create(:user) }
58+
59+
it 'returns forbidden' do
60+
get '/api/v1/families/locations', params: { api_key: solo_user.api_key }
61+
62+
expect(response).to have_http_status(:forbidden)
63+
json_response = JSON.parse(response.body)
64+
expect(json_response['error']).to eq('User is not part of a family')
65+
end
66+
end
67+
end
68+
69+
describe 'PATCH /api/v1/families/locations/toggle' do
70+
before { sign_in user }
71+
72+
context 'with valid session authentication' do
73+
context 'when enabling location sharing' do
74+
around do |example|
75+
travel_to(Time.zone.local(2024, 1, 1, 12, 0, 0)) { example.run }
76+
end
77+
78+
it 'enables location sharing with duration' do
79+
patch '/api/v1/families/locations/toggle',
80+
params: { enabled: true, duration: '1h' },
81+
as: :json
82+
83+
expect(response).to have_http_status(:ok)
84+
json_response = JSON.parse(response.body)
85+
expect(json_response['success']).to be true
86+
expect(json_response['enabled']).to be true
87+
expect(json_response['duration']).to eq('1h')
88+
expect(json_response['message']).to eq('Location sharing enabled for 1 hour')
89+
expect(json_response['expires_at']).to eq(1.hour.from_now.iso8601)
90+
end
91+
92+
it 'enables location sharing permanently' do
93+
patch '/api/v1/families/locations/toggle',
94+
params: { enabled: true, duration: 'permanent' },
95+
as: :json
96+
97+
expect(response).to have_http_status(:ok)
98+
json_response = JSON.parse(response.body)
99+
expect(json_response['success']).to be true
100+
expect(json_response['enabled']).to be true
101+
expect(json_response['duration']).to eq('permanent')
102+
expect(json_response).not_to have_key('expires_at')
103+
end
104+
end
105+
106+
context 'when disabling location sharing' do
107+
before do
108+
user.update_family_location_sharing!(true, duration: '1h')
109+
end
110+
111+
it 'disables location sharing' do
112+
patch '/api/v1/families/locations/toggle',
113+
params: { enabled: false },
114+
as: :json
115+
116+
expect(response).to have_http_status(:ok)
117+
json_response = JSON.parse(response.body)
118+
expect(json_response['success']).to be true
119+
expect(json_response['enabled']).to be false
120+
expect(json_response['message']).to eq('Location sharing disabled')
121+
end
122+
end
123+
124+
context 'when user is not in a family' do
125+
let(:solo_user) { create(:user) }
126+
127+
before do
128+
sign_out user
129+
sign_in solo_user
130+
end
131+
132+
it 'returns forbidden' do
133+
patch '/api/v1/families/locations/toggle',
134+
params: { enabled: true, duration: '1h' },
135+
as: :json
136+
137+
expect(response).to have_http_status(:forbidden)
138+
json_response = JSON.parse(response.body)
139+
expect(json_response['error']).to eq('User is not part of a family')
140+
end
141+
end
142+
143+
context 'when update fails' do
144+
before do
145+
allow_any_instance_of(User).to receive(:update_family_location_sharing!)
146+
.and_raise(StandardError, 'Database error')
147+
end
148+
149+
it 'returns internal server error' do
150+
patch '/api/v1/families/locations/toggle',
151+
params: { enabled: true, duration: '1h' },
152+
as: :json
153+
154+
expect(response).to have_http_status(:internal_server_error)
155+
json_response = JSON.parse(response.body)
156+
expect(json_response['success']).to be false
157+
expect(json_response['message']).to eq('An error occurred while updating location sharing')
158+
end
159+
end
160+
end
161+
162+
context 'without authentication' do
163+
before { sign_out user }
164+
165+
it 'returns unauthorized' do
166+
patch '/api/v1/families/locations/toggle',
167+
params: { enabled: true, duration: '1h' },
168+
as: :json
169+
170+
expect(response).to have_http_status(:unauthorized)
171+
end
172+
end
173+
end
174+
end

0 commit comments

Comments
 (0)