From 992f544256faf6f8c876c725140556b78be964ac Mon Sep 17 00:00:00 2001 From: Wilco van Beijnum Date: Fri, 29 Mar 2024 16:26:10 +0100 Subject: [PATCH 1/5] Add feature of tagging users in photos --- app/controllers/v1/photo_tags_controller.rb | 2 + app/models/photo.rb | 6 ++ app/models/photo_tag.rb | 20 +++++++ app/models/user.rb | 4 ++ app/policies/photo_tag_policy.rb | 35 ++++++++++++ app/resources/v1/photo_resource.rb | 13 ++++- app/resources/v1/photo_tag_resource.rb | 16 ++++++ app/resources/v1/user_resource.rb | 3 +- config/routes.rb | 1 + .../20240328205012_create_photo_tags.rb | 20 +++++++ db/schema.rb | 14 ++++- db/seeds/permissions.rb | 3 + spec/factories/photo_tags.rb | 10 ++++ spec/jobs/user_archive_job_spec.rb | 3 + spec/models/photo_spec.rb | 15 +++++ spec/models/photo_tag_spec.rb | 57 +++++++++++++++++++ spec/policies/photo_tag_policy_spec.rb | 37 ++++++++++++ .../v1/photo_tags_controller/create_spec.rb | 53 +++++++++++++++++ .../v1/photo_tags_controller/destroy_spec.rb | 11 ++++ .../v1/photo_tags_controller/index_spec.rb | 13 +++++ .../v1/photo_tags_controller/show_spec.rb | 11 ++++ spec/resources/v1/photo_resource_spec.rb | 13 +++++ 22 files changed, 355 insertions(+), 5 deletions(-) create mode 100644 app/controllers/v1/photo_tags_controller.rb create mode 100644 app/models/photo_tag.rb create mode 100644 app/policies/photo_tag_policy.rb create mode 100644 app/resources/v1/photo_tag_resource.rb create mode 100644 db/migrate/20240328205012_create_photo_tags.rb create mode 100644 spec/factories/photo_tags.rb create mode 100644 spec/models/photo_tag_spec.rb create mode 100644 spec/policies/photo_tag_policy_spec.rb create mode 100644 spec/requests/v1/photo_tags_controller/create_spec.rb create mode 100644 spec/requests/v1/photo_tags_controller/destroy_spec.rb create mode 100644 spec/requests/v1/photo_tags_controller/index_spec.rb create mode 100644 spec/requests/v1/photo_tags_controller/show_spec.rb diff --git a/app/controllers/v1/photo_tags_controller.rb b/app/controllers/v1/photo_tags_controller.rb new file mode 100644 index 00000000..5aefb485 --- /dev/null +++ b/app/controllers/v1/photo_tags_controller.rb @@ -0,0 +1,2 @@ +class V1::PhotoTagsController < V1::ApplicationController +end diff --git a/app/models/photo.rb b/app/models/photo.rb index 3c484ae8..387b76a8 100644 --- a/app/models/photo.rb +++ b/app/models/photo.rb @@ -8,6 +8,8 @@ class Photo < ApplicationRecord belongs_to :uploader, class_name: 'User' has_many :comments, class_name: 'PhotoComment', dependent: :destroy, counter_cache: :comments_count + has_many :tags, class_name: 'PhotoTag', + dependent: :destroy, counter_cache: :tags_count mount_uploader :image, PhotoUploader has_paper_trail skip: [:image] @@ -19,6 +21,10 @@ class Photo < ApplicationRecord joins(:comments).distinct }) + scope :with_tags, (lambda { + joins(:tags).distinct + }) + scope :publicly_visible, (lambda { joins(:photo_album).where(photo_albums: { publicly_visible: true }) }) diff --git a/app/models/photo_tag.rb b/app/models/photo_tag.rb new file mode 100644 index 00000000..9ce9d748 --- /dev/null +++ b/app/models/photo_tag.rb @@ -0,0 +1,20 @@ +class PhotoTag < ApplicationRecord + belongs_to :photo, touch: true + counter_culture :photo, column_name: :tags_count + belongs_to :author, class_name: 'User' + belongs_to :tagged_user, class_name: 'User' + + validates :x, inclusion: { in: 0.0..100.0 } + validates :y, inclusion: { in: 0.0..100.0 } + + validate :user_not_already_tagged + + private + + def user_not_already_tagged + if PhotoTag.exists?(photo_id: photo_id, tagged_user_id: tagged_user_id) + errors.add(:tagged_user, "has already been tagged in this photo") + end + end + +end diff --git a/app/models/user.rb b/app/models/user.rb index 0acff4c3..c71e6282 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -17,6 +17,10 @@ class User < ApplicationRecord # rubocop:disable Metrics/ClassLength has_many :article_comments, foreign_key: :author_id has_many :board_room_presences, dependent: :delete_all has_many :photo_comments, foreign_key: :author_id + has_many :created_photo_tags, class_name: 'PhotoTag', foreign_key: :author_id, + dependent: :delete_all + has_many :photo_tags, foreign_key: :tagged_user_id, dependent: :delete_all + has_many :photos, through: :photo_tags has_many :mail_aliases, dependent: :delete_all has_many :read_threads, class_name: 'Forum::ReadThread', dependent: :delete_all has_many :mandates, class_name: 'Debit::Mandate', dependent: :delete_all diff --git a/app/policies/photo_tag_policy.rb b/app/policies/photo_tag_policy.rb new file mode 100644 index 00000000..e98ef976 --- /dev/null +++ b/app/policies/photo_tag_policy.rb @@ -0,0 +1,35 @@ +class PhotoTagPolicy < ApplicationPolicy + def update? + user_is_owner? || user_is_tagged? || super + end + + def destroy? + user_is_owner? || user_is_tagged? || super + end + + def create_with_photo?(_photo) + true + end + + def replace_photo?(_photo) + true + end + + def create_with_tagged_user?(_user) + true + end + + def replace_tagged_user?(_user) + true + end + + private + + def user_is_owner? + record.author == user + end + + def user_is_tagged? + record.tagged_user == user + end +end diff --git a/app/resources/v1/photo_resource.rb b/app/resources/v1/photo_resource.rb index 7e50c9a6..484695ed 100644 --- a/app/resources/v1/photo_resource.rb +++ b/app/resources/v1/photo_resource.rb @@ -1,7 +1,8 @@ class V1::PhotoResource < V1::ApplicationResource - attributes :image_url, :image_thumb_url, :image_medium_url, :amount_of_comments, :exif_make, - :exif_model, :exif_date_time_original, :exif_exposure_time, :exif_aperture_value, - :exif_iso_speed_ratings, :exif_copyright, :exif_lens_model, :exif_focal_length + attributes :image_url, :image_thumb_url, :image_medium_url, :amount_of_comments, :amount_of_tags, + :exif_make, :exif_model, :exif_date_time_original, :exif_exposure_time, + :exif_aperture_value, :exif_iso_speed_ratings, :exif_copyright, :exif_lens_model, + :exif_focal_length def image_thumb_url @model.image.thumb.url @@ -15,9 +16,15 @@ def amount_of_comments @model.comments.size end + def amount_of_tags + @model.tags.size + end + filter :with_comments, apply: ->(records, _value, _options) { records.with_comments } + filter :with_tags, apply: ->(records, _value, _options) { records.with_tags } has_one :photo_album, always_include_linkage_data: true has_one :uploader, always_include_linkage_data: true has_many :comments + has_many :tags end diff --git a/app/resources/v1/photo_tag_resource.rb b/app/resources/v1/photo_tag_resource.rb new file mode 100644 index 00000000..a5d1abd1 --- /dev/null +++ b/app/resources/v1/photo_tag_resource.rb @@ -0,0 +1,16 @@ +class V1::PhotoTagResource < V1::ApplicationResource + attributes :x + attributes :y + + has_one :photo, always_include_linkage_data: true + has_one :author, always_include_linkage_data: true + has_one :tagged_user, always_include_linkage_data: true + + before_create do + @model.author_id = current_user.id + end + + def self.creatable_fields(_context) + %i[x y photo tagged_user] + end +end diff --git a/app/resources/v1/user_resource.rb b/app/resources/v1/user_resource.rb index 629fba82..bef3359f 100644 --- a/app/resources/v1/user_resource.rb +++ b/app/resources/v1/user_resource.rb @@ -23,6 +23,7 @@ def avatar_thumb_url has_many :mandates, always_include_linkage_data: true has_many :group_mail_aliases has_many :permissions + has_many :photos has_many :user_permissions filter :upcoming_birthdays, apply: lambda { |records, _value, options| @@ -50,7 +51,7 @@ def fetchable_fields avatar_url avatar_thumb_url created_at updated_at id] # Relationships allowed_keys += %i[groups active_groups memberships mail_aliases mandates - group_mail_aliases permissions user_permissions] + group_mail_aliases permissions photos user_permissions] allowed_keys += %i[ical_secret_key webdav_secret_key] if me? if update_or_me? allowed_keys += %i[login_enabled otp_required activated_at emergency_contact diff --git a/config/routes.rb b/config/routes.rb index 33f9b09e..20b919f4 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -42,6 +42,7 @@ end end jsonapi_resources :photo_comments + jsonapi_resources :photo_tags jsonapi_resources :photos, only: %i[index show destroy] jsonapi_resources :polls jsonapi_resources :room_adverts diff --git a/db/migrate/20240328205012_create_photo_tags.rb b/db/migrate/20240328205012_create_photo_tags.rb new file mode 100644 index 00000000..17bd979b --- /dev/null +++ b/db/migrate/20240328205012_create_photo_tags.rb @@ -0,0 +1,20 @@ +class CreatePhotoTags < ActiveRecord::Migration[7.0] + def change + create_table :photo_tags do |t| + t.decimal :x, precision: 5, scale: 2 + t.float :y, precision: 5, scale: 2 + t.integer :author_id, null: false + t.integer :tagged_user_id, null: false + t.integer :photo_id, null: false + t.datetime :deleted_at + + t.timestamps + end + add_column :photos, :tags_count, :integer, null: false, default: 0 + end + + Permission.create(name: 'photo_tag.create') + Permission.create(name: 'photo_tag.read') + Permission.create(name: 'photo_tag.update') + Permission.create(name: 'photo_tag.destroy') +end diff --git a/db/schema.rb b/db/schema.rb index c16453c8..3cb6bb07 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2023_07_27_114709) do +ActiveRecord::Schema[7.0].define(version: 2024_03_28_205012) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -430,6 +430,17 @@ t.index ["photo_id"], name: "index_photo_comments_on_photo_id" end + create_table "photo_tags", force: :cascade do |t| + t.decimal "x", precision: 5, scale: 2 + t.float "y" + t.integer "author_id", null: false + t.integer "tagged_user_id", null: false + t.integer "photo_id", null: false + t.datetime "deleted_at" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "photos", id: :serial, force: :cascade do |t| t.string "image" t.integer "photo_album_id" @@ -448,6 +459,7 @@ t.string "exif_copyright" t.string "exif_lens_model" t.integer "exif_focal_length" + t.integer "tags_count", default: 0, null: false t.index ["deleted_at"], name: "index_photos_on_deleted_at" t.index ["photo_album_id"], name: "index_photos_on_photo_album_id" t.index ["uploader_id"], name: "index_photos_on_uploader_id" diff --git a/db/seeds/permissions.rb b/db/seeds/permissions.rb index 08cdc412..85bb9579 100644 --- a/db/seeds/permissions.rb +++ b/db/seeds/permissions.rb @@ -32,6 +32,7 @@ def create_permissions(permission_map) 'photo_album' => %i[create read update destroy], 'photo' => %i[create read update destroy], 'photo_comment' => %i[create read update destroy], + 'photo_tag' => %i[create read update destroy], 'quickpost_message' => %i[create read update destroy], 'room_advert' => %i[create read update destroy], 'stored_mail' => %i[read destroy], @@ -72,6 +73,7 @@ def create_permissions(permission_map) 'photo_album' => %i[create read], 'photo' => %i[create read], 'photo_comment' => %i[create read], + 'photo_tag' => %i[create read], 'quickpost_message' => %i[create read], 'room_advert' => %i[create read], 'forum/category' => %i[read], @@ -105,6 +107,7 @@ def create_permissions(permission_map) 'photo_album' => %i[read], 'photo' => %i[read], 'photo_comment' => %i[create read], + 'photo_tag' => %i[create read], 'quickpost_message' => %i[create read], 'room_advert' => %i[], 'forum/category' => %i[read], diff --git a/spec/factories/photo_tags.rb b/spec/factories/photo_tags.rb new file mode 100644 index 00000000..91053749 --- /dev/null +++ b/spec/factories/photo_tags.rb @@ -0,0 +1,10 @@ +FactoryBot.define do + factory :photo_tag do + x { rand(0.0..100.0) } + y { rand(0.0..100.0) } + association :author, factory: :user + association :tagged_user, factory: :user + + photo + end +end diff --git a/spec/jobs/user_archive_job_spec.rb b/spec/jobs/user_archive_job_spec.rb index 20325921..70f84135 100644 --- a/spec/jobs/user_archive_job_spec.rb +++ b/spec/jobs/user_archive_job_spec.rb @@ -47,6 +47,8 @@ create(:mail_alias, user: user) create(:membership, user: user) create(:permissions_users, user: user) + create(:photo_tag, author: user) + create(:photo_tag, tagged_user: user) end it { expect { job }.to change(BoardRoomPresence, :count).by(-1) } @@ -55,6 +57,7 @@ it { expect { job }.to change(MailAlias, :count).by(-1) } it { expect { job }.to change(Membership, :count).by(-1) } it { expect { job }.to change(PermissionsUsers, :count).by(-1) } + it { expect { job }.to change(PhotoTag, :count).by(-2) } it { expect { job }.to change { User.exists?(user.id) }.from(true).to(false) } end end diff --git a/spec/models/photo_spec.rb b/spec/models/photo_spec.rb index d26b5615..8e0142c8 100644 --- a/spec/models/photo_spec.rb +++ b/spec/models/photo_spec.rb @@ -46,6 +46,21 @@ it { expect(described_class.with_comments.count).to be 2 } end + describe '#with_tags' do + let(:photo_with_tags) { create(:photo) } + let(:photo_with_one_tag) { create(:photo) } + + before do + create(:photo_tag, photo: photo_with_tags) + create(:photo_tag, photo: photo_with_tags) + create(:photo_tag, photo: photo_with_one_tag) + create(:photo) + end + + it { expect(described_class.count).to be 3 } + it { expect(described_class.with_tags.count).to be 2 } + end + describe '#publicly_visible' do let(:public_album) { create(:photo_album, publicly_visible: true) } let(:private_album) { create(:photo_album, publicly_visible: false) } diff --git a/spec/models/photo_tag_spec.rb b/spec/models/photo_tag_spec.rb new file mode 100644 index 00000000..6f38cc80 --- /dev/null +++ b/spec/models/photo_tag_spec.rb @@ -0,0 +1,57 @@ +require 'rails_helper' + +RSpec.describe PhotoTag, type: :model do + subject(:photo_tag) { build(:photo_tag) } + + describe '#valid?' do + it { expect(photo_tag).to be_valid } + + context 'when without an x position' do + subject(:photo_tag) { build(:photo_tag, x: nil) } + + it { expect(photo_tag).not_to be_valid } + end + + context 'when without an y position' do + subject(:photo_tag) { build(:photo_tag, y: nil) } + + it { expect(photo_tag).not_to be_valid } + end + + context 'when with a too large x position' do + subject(:photo_tag) { build(:photo_tag, x: 101) } + + it { expect(photo_tag).not_to be_valid } + end + + context 'when with a too small x position' do + subject(:photo_tag) { build(:photo_tag, x: -1) } + + it { expect(photo_tag).not_to be_valid } + end + + context 'when with a too large y position' do + subject(:photo_tag) { build(:photo_tag, y: 101) } + + it { expect(photo_tag).not_to be_valid } + end + + context 'when with a too small y position' do + subject(:photo_tag) { build(:photo_tag, y: -1) } + + it { expect(photo_tag).not_to be_valid } + end + + context 'when with a valid x position' do + subject(:photo_tag) { build(:photo_tag, x: 50) } + + it { expect(photo_tag).to be_valid } + end + + context 'when with a valid y position' do + subject(:photo_tag) { build(:photo_tag, y: 50) } + + it { expect(photo_tag).to be_valid } + end + end +end diff --git a/spec/policies/photo_tag_policy_spec.rb b/spec/policies/photo_tag_policy_spec.rb new file mode 100644 index 00000000..b07b9ea2 --- /dev/null +++ b/spec/policies/photo_tag_policy_spec.rb @@ -0,0 +1,37 @@ +require 'rails_helper' + +RSpec.describe PhotoTagPolicy, type: :policy do + subject(:policy) { described_class } + + let(:user) { build(:user) } + + permissions :update? do + describe 'when photo tag is not owned or tagged' do + it { expect(policy).not_to permit(user, build(:photo_tag)) } + end + + describe 'when photo tag is owned' do + it { expect(policy).to permit(user, build(:photo_tag, author: user)) } + end + + describe 'when photo tag is tagged' do + it { expect(policy).to permit(user, build(:photo_tag, tagged_user: user)) } + end + end + + describe '#create_with_photo?' do + it { expect(policy.new(nil, nil).create_with_photo?(nil)).to be true } + end + + describe '#replace_photo?' do + it { expect(policy.new(nil, nil).replace_photo?(nil)).to be true } + end + + describe '#create_with_tagged_user?' do + it { expect(policy.new(nil, nil).create_with_tagged_user?(nil)).to be true } + end + + describe '#replace_tagged_user?' do + it { expect(policy.new(nil, nil).replace_tagged_user?(nil)).to be true } + end +end diff --git a/spec/requests/v1/photo_tags_controller/create_spec.rb b/spec/requests/v1/photo_tags_controller/create_spec.rb new file mode 100644 index 00000000..e79c1cb8 --- /dev/null +++ b/spec/requests/v1/photo_tags_controller/create_spec.rb @@ -0,0 +1,53 @@ +require 'rails_helper' + +describe V1::PhotoTagsController do + describe 'POST /photo_tags', version: 1 do + let(:record) { create(:photo_tag) } + let(:record_url) { '/v1/photo_tags' } + let(:record_permission) { 'photo_tag.create' } + + it_behaves_like 'a creatable and permissible model' do + let(:valid_attributes) { record.attributes } + let(:valid_relationships) do + { + photo: { data: { id: record.photo_id, type: 'photos' } }, + tagged_user: { data: { id: record.tagged_user_id, type: 'users' } } + } + end + let(:invalid_relationships) do + { + photo: { data: { id: nil, type: 'photos' } }, + tagged_user: { data: { id: nil, type: 'users' } } + } + end + end + + context 'when authenticated' do + let(:another_user) { create(:user) } + let(:valid_request) do + post( + record_url, + data: { + id: record.id, + type: record_type(record), + attributes: record.attributes, + relationships: { + photo: { data: { id: record.photo_id, type: 'photos' } }, + user: { data: { id: another_user.id, type: 'users' } }, + tagged_user: { data: { id: record.tagged_user_id, type: 'users' } } + } + } + ) + end + + include_context 'when authenticated' do + let(:user) { create(:user, user_permission_list: [record_permission]) } + end + + before { valid_request } + + it { expect(valid_request.status).to eq(201) } + it { expect(record.class.last.author).to eq(user) } + end + end +end diff --git a/spec/requests/v1/photo_tags_controller/destroy_spec.rb b/spec/requests/v1/photo_tags_controller/destroy_spec.rb new file mode 100644 index 00000000..98726fb6 --- /dev/null +++ b/spec/requests/v1/photo_tags_controller/destroy_spec.rb @@ -0,0 +1,11 @@ +require 'rails_helper' + +describe V1::PhotoTagsController do + describe 'DELETE /photo_tags/:id', version: 1 do + it_behaves_like 'a destroyable and permissible model' do + let(:record) { create(:photo_tag) } + let(:record_url) { "/v1/photo_tags/#{record.id}" } + let(:record_permission) { 'photo_tag.destroy' } + end + end +end diff --git a/spec/requests/v1/photo_tags_controller/index_spec.rb b/spec/requests/v1/photo_tags_controller/index_spec.rb new file mode 100644 index 00000000..1fb95836 --- /dev/null +++ b/spec/requests/v1/photo_tags_controller/index_spec.rb @@ -0,0 +1,13 @@ +require 'rails_helper' + +describe V1::PhotoTagsController do + describe 'GET /photo_tags', version: 1 do + let(:records) { create_list(:photo_tag, 3) } + let(:record_url) { '/v1/photo_tags' } + let(:record_permission) { 'photo_tag.read' } + let(:request) { get(record_url) } + + it_behaves_like 'a permissible model' + it_behaves_like 'an indexable model' + end +end diff --git a/spec/requests/v1/photo_tags_controller/show_spec.rb b/spec/requests/v1/photo_tags_controller/show_spec.rb new file mode 100644 index 00000000..1f46520b --- /dev/null +++ b/spec/requests/v1/photo_tags_controller/show_spec.rb @@ -0,0 +1,11 @@ +require 'rails_helper' + +describe V1::PhotoTagsController do + describe 'GET /photo_tags/:id', version: 1 do + it_behaves_like 'a permissible model' do + let(:record) { create(:photo_tag) } + let(:record_url) { "/v1/photo_tags/#{record.id}" } + let(:record_permission) { 'photo_tag.read' } + end + end +end diff --git a/spec/resources/v1/photo_resource_spec.rb b/spec/resources/v1/photo_resource_spec.rb index 38244ec5..5da5f35e 100644 --- a/spec/resources/v1/photo_resource_spec.rb +++ b/spec/resources/v1/photo_resource_spec.rb @@ -25,5 +25,18 @@ it { expect(resource.amount_of_comments).to eq(3) } end + + context 'when with tags' do + let(:photo) { create(:photo) } + + before do + create(:photo_tag, photo: photo) + create(:photo_tag, photo: photo) + create(:photo_tag, photo: photo) + photo.reload + end + + it { expect(resource.amount_of_tags).to eq(3) } + end end end From 6a99a51dbf436cb52ded567560d395ab7c86cd5a Mon Sep 17 00:00:00 2001 From: Wilco van Beijnum Date: Fri, 29 Mar 2024 16:57:14 +0100 Subject: [PATCH 2/5] Fix lint --- app/models/photo_tag.rb | 5 ++--- app/models/user.rb | 2 +- spec/resources/v1/user_resource_spec.rb | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/app/models/photo_tag.rb b/app/models/photo_tag.rb index 9ce9d748..68b34788 100644 --- a/app/models/photo_tag.rb +++ b/app/models/photo_tag.rb @@ -6,15 +6,14 @@ class PhotoTag < ApplicationRecord validates :x, inclusion: { in: 0.0..100.0 } validates :y, inclusion: { in: 0.0..100.0 } - + validate :user_not_already_tagged private def user_not_already_tagged if PhotoTag.exists?(photo_id: photo_id, tagged_user_id: tagged_user_id) - errors.add(:tagged_user, "has already been tagged in this photo") + errors.add(:tagged_user, 'has already been tagged in this photo') end end - end diff --git a/app/models/user.rb b/app/models/user.rb index c71e6282..6cc50754 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -18,7 +18,7 @@ class User < ApplicationRecord # rubocop:disable Metrics/ClassLength has_many :board_room_presences, dependent: :delete_all has_many :photo_comments, foreign_key: :author_id has_many :created_photo_tags, class_name: 'PhotoTag', foreign_key: :author_id, - dependent: :delete_all + dependent: :delete_all has_many :photo_tags, foreign_key: :tagged_user_id, dependent: :delete_all has_many :photos, through: :photo_tags has_many :mail_aliases, dependent: :delete_all diff --git a/spec/resources/v1/user_resource_spec.rb b/spec/resources/v1/user_resource_spec.rb index 3a7f044d..9392f1e1 100644 --- a/spec/resources/v1/user_resource_spec.rb +++ b/spec/resources/v1/user_resource_spec.rb @@ -137,7 +137,7 @@ digtus_subscription_preference emergency_contact emergency_number] end let(:permissible_fields) do - %i[first_name last_name_prefix last_name birthday + %i[first_name last_name_prefix last_name birthday photos user_permissions login_enabled] end let(:current_user_fields) do From 3835fdaf5f2dd79f7465ae4cdba796c9486ff93d Mon Sep 17 00:00:00 2001 From: Wilco van Beijnum Date: Fri, 29 Mar 2024 17:03:12 +0100 Subject: [PATCH 3/5] Fix tests --- spec/requests/v1/photo_tags_controller/create_spec.rb | 4 ++-- spec/resources/v1/user_resource_spec.rb | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/spec/requests/v1/photo_tags_controller/create_spec.rb b/spec/requests/v1/photo_tags_controller/create_spec.rb index e79c1cb8..1ade9476 100644 --- a/spec/requests/v1/photo_tags_controller/create_spec.rb +++ b/spec/requests/v1/photo_tags_controller/create_spec.rb @@ -11,7 +11,7 @@ let(:valid_relationships) do { photo: { data: { id: record.photo_id, type: 'photos' } }, - tagged_user: { data: { id: record.tagged_user_id, type: 'users' } } + tagged_user: { data: { id: create(:user).id, type: 'users' } } } end let(:invalid_relationships) do @@ -34,7 +34,7 @@ relationships: { photo: { data: { id: record.photo_id, type: 'photos' } }, user: { data: { id: another_user.id, type: 'users' } }, - tagged_user: { data: { id: record.tagged_user_id, type: 'users' } } + tagged_user: { data: { id: create(:user).id, type: 'users' } } } } ) diff --git a/spec/resources/v1/user_resource_spec.rb b/spec/resources/v1/user_resource_spec.rb index 9392f1e1..5086002b 100644 --- a/spec/resources/v1/user_resource_spec.rb +++ b/spec/resources/v1/user_resource_spec.rb @@ -9,7 +9,7 @@ let(:basic_fields) do %i[id created_at updated_at username first_name last_name_prefix last_name full_name nickname avatar_url avatar_thumb_url groups active_groups memberships mail_aliases - group_mail_aliases permissions user_permissions mandates] + group_mail_aliases photos permissions user_permissions mandates] end let(:update_fields) do %i[login_enabled otp_required activated_at emergency_contact @@ -137,7 +137,7 @@ digtus_subscription_preference emergency_contact emergency_number] end let(:permissible_fields) do - %i[first_name last_name_prefix last_name birthday photos + %i[first_name last_name_prefix last_name birthday user_permissions login_enabled] end let(:current_user_fields) do From 1da4369eba07060ad21c9efbe4deb6ee1f9b90c0 Mon Sep 17 00:00:00 2001 From: Wilco van Beijnum Date: Fri, 29 Mar 2024 21:53:46 +0100 Subject: [PATCH 4/5] Increase coverage --- spec/models/photo_tag_spec.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/spec/models/photo_tag_spec.rb b/spec/models/photo_tag_spec.rb index 6f38cc80..443a8105 100644 --- a/spec/models/photo_tag_spec.rb +++ b/spec/models/photo_tag_spec.rb @@ -53,5 +53,14 @@ it { expect(photo_tag).to be_valid } end + + context 'when with a user that is already tagged' do + let(:photo) { create(:photo) } + let(:tagged_user) { create(:user) } + before { create(:photo_tag, photo_id: photo.id, tagged_user_id: tagged_user.id ) } + subject(:photo_tag) { build(:photo_tag, photo_id: photo.id, tagged_user_id: tagged_user.id ) } + + it { expect(photo_tag).not_to be_valid } + end end end From 68563c4521d3c28806dca4b86664f744ff428c1a Mon Sep 17 00:00:00 2001 From: Wilco Date: Fri, 29 Mar 2024 23:17:04 +0100 Subject: [PATCH 5/5] Fix lint --- spec/models/photo_tag_spec.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/spec/models/photo_tag_spec.rb b/spec/models/photo_tag_spec.rb index 443a8105..a2734e18 100644 --- a/spec/models/photo_tag_spec.rb +++ b/spec/models/photo_tag_spec.rb @@ -57,8 +57,10 @@ context 'when with a user that is already tagged' do let(:photo) { create(:photo) } let(:tagged_user) { create(:user) } - before { create(:photo_tag, photo_id: photo.id, tagged_user_id: tagged_user.id ) } - subject(:photo_tag) { build(:photo_tag, photo_id: photo.id, tagged_user_id: tagged_user.id ) } + + before { create(:photo_tag, photo_id: photo.id, tagged_user_id: tagged_user.id) } + + subject(:photo_tag) { build(:photo_tag, photo_id: photo.id, tagged_user_id: tagged_user.id) } it { expect(photo_tag).not_to be_valid } end