Fix some tests

This commit is contained in:
Eugene Burmakin 2025-11-19 20:08:49 +01:00
parent 1d07eb652d
commit e02b397b87
5 changed files with 32 additions and 29 deletions

View file

@ -24,6 +24,7 @@ OIDC_REDIRECT_URI=https://your-dawarich-url.com/users/auth/openid_connect/callba
- User can now create a place directly from the map and add tags and notes to it. If reverse geocoding is enabled, list of nearby places will be shown as suggestions. - User can now create a place directly from the map and add tags and notes to it. If reverse geocoding is enabled, list of nearby places will be shown as suggestions.
- User can create and manage tags for places. - User can create and manage tags for places.
- [ ] Tags can be added when creating or editing a place. - [ ] Tags can be added when creating or editing a place.
- [ ] Provide sensible list of default tags for the user to choose from.
- User can enable or disable places layers on the map to show/hide all or just some of their places based on tags. - User can enable or disable places layers on the map to show/hide all or just some of their places based on tags.
- User can define privacy zones around places with specific tags to hide map data within a certain radius. - User can define privacy zones around places with specific tags to hide map data within a certain radius.

View file

@ -6,7 +6,7 @@ module Api
before_action :set_place, only: [:show, :update, :destroy] before_action :set_place, only: [:show, :update, :destroy]
def index def index
@places = policy_scope(Place).includes(:tags, :visits) @places = current_api_user.places.includes(:tags, :visits)
@places = @places.with_tags(params[:tag_ids]) if params[:tag_ids].present? @places = @places.with_tags(params[:tag_ids]) if params[:tag_ids].present?
@places = @places.without_tags if params[:untagged] == 'true' @places = @places.without_tags if params[:untagged] == 'true'
@ -14,16 +14,12 @@ module Api
end end
def show def show
authorize @place
render json: serialize_place(@place) render json: serialize_place(@place)
end end
def create def create
@place = current_api_user.places.build(place_params) @place = current_api_user.places.build(place_params)
authorize @place
if @place.save if @place.save
add_tags if tag_ids.present? add_tags if tag_ids.present?
render json: serialize_place(@place), status: :created render json: serialize_place(@place), status: :created
@ -33,8 +29,6 @@ module Api
end end
def update def update
authorize @place
if @place.update(place_params) if @place.update(place_params)
set_tags if params[:place][:tag_ids] set_tags if params[:place][:tag_ids]
render json: serialize_place(@place) render json: serialize_place(@place)
@ -44,16 +38,12 @@ module Api
end end
def destroy def destroy
authorize @place
@place.destroy! @place.destroy!
head :no_content head :no_content
end end
def nearby def nearby
authorize Place, :nearby?
unless params[:latitude].present? && params[:longitude].present? unless params[:latitude].present? && params[:longitude].present?
return render json: { error: 'latitude and longitude are required' }, status: :bad_request return render json: { error: 'latitude and longitude are required' }, status: :bad_request
end end
@ -99,8 +89,8 @@ module Api
{ {
id: place.id, id: place.id,
name: place.name, name: place.name,
latitude: place.latitude, latitude: place.lat,
longitude: place.longitude, longitude: place.lon,
source: place.source, source: place.source,
note: place.note, note: place.note,
icon: place.tags.first&.icon, icon: place.tags.first&.icon,

View file

@ -5,8 +5,14 @@ class ApiController < ApplicationController
before_action :set_version_header before_action :set_version_header
before_action :authenticate_api_key before_action :authenticate_api_key
rescue_from ActiveRecord::RecordNotFound, with: :record_not_found
private private
def record_not_found
render json: { error: 'Record not found' }, status: :not_found
end
def set_version_header def set_version_header
message = "Hey, I\'m alive#{current_api_user ? ' and authenticated' : ''}!" message = "Hey, I\'m alive#{current_api_user ? ' and authenticated' : ''}!"

View file

@ -11,7 +11,7 @@ RSpec.describe User, type: :model do
it { is_expected.to have_many(:notifications).dependent(:destroy) } it { is_expected.to have_many(:notifications).dependent(:destroy) }
it { is_expected.to have_many(:areas).dependent(:destroy) } it { is_expected.to have_many(:areas).dependent(:destroy) }
it { is_expected.to have_many(:visits).dependent(:destroy) } it { is_expected.to have_many(:visits).dependent(:destroy) }
it { is_expected.to have_many(:places).through(:visits) } it { is_expected.to have_many(:places).dependent(:destroy) }
it { is_expected.to have_many(:trips).dependent(:destroy) } it { is_expected.to have_many(:trips).dependent(:destroy) }
it { is_expected.to have_many(:tracks).dependent(:destroy) } it { is_expected.to have_many(:tracks).dependent(:destroy) }
it { is_expected.to have_many(:tags).dependent(:destroy) } it { is_expected.to have_many(:tags).dependent(:destroy) }

View file

@ -51,11 +51,12 @@ RSpec.describe 'Api::V1::Places', type: :request do
end end
it 'returns 404 for other users place' do it 'returns 404 for other users place' do
other_place = create(:place, user: create(:user)) other_user = create(:user)
other_place = create(:place, user: other_user)
expect {
get "/api/v1/places/#{other_place.id}", headers: headers get "/api/v1/places/#{other_place.id}", headers: headers
}.to raise_error(ActiveRecord::RecordNotFound)
expect(response).to have_http_status(:not_found)
end end
end end
@ -119,13 +120,15 @@ RSpec.describe 'Api::V1::Places', type: :request do
end end
it 'prevents updating other users places' do it 'prevents updating other users places' do
other_place = create(:place, user: create(:user)) other_user = create(:user)
other_place = create(:place, user: other_user)
expect {
patch "/api/v1/places/#{other_place.id}", patch "/api/v1/places/#{other_place.id}",
params: { place: { name: 'Hacked' } }, params: { place: { name: 'Hacked' } },
headers: headers headers: headers
}.to raise_error(ActiveRecord::RecordNotFound)
expect(response).to have_http_status(:not_found)
expect(other_place.reload.name).not_to eq('Hacked')
end end
end end
@ -139,11 +142,14 @@ RSpec.describe 'Api::V1::Places', type: :request do
end end
it 'prevents deleting other users places' do it 'prevents deleting other users places' do
other_place = create(:place, user: create(:user)) other_user = create(:user)
other_place = create(:place, user: other_user)
expect { expect {
delete "/api/v1/places/#{other_place.id}", headers: headers delete "/api/v1/places/#{other_place.id}", headers: headers
}.to raise_error(ActiveRecord::RecordNotFound) }.not_to change(Place, :count)
expect(response).to have_http_status(:not_found)
end end
end end