|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'rails_helper' |
| 4 | + |
| 5 | +RSpec.describe 'ApiManage::V1::SppListings#update', type: :request do |
| 6 | + let!(:website) { create(:pwb_website) } |
| 7 | + let!(:user) { create(:pwb_user, :admin, website: website) } |
| 8 | + let!(:property) { create(:pwb_realty_asset, website: website) } |
| 9 | + let!(:spp_listing) do |
| 10 | + create(:pwb_spp_listing, :published, |
| 11 | + realty_asset: property, |
| 12 | + listing_type: 'sale') |
| 13 | + end |
| 14 | + |
| 15 | + let(:auth_headers) do |
| 16 | + { |
| 17 | + 'HTTP_HOST' => "#{website.subdomain}.localhost", |
| 18 | + 'X-User-Email' => user.email |
| 19 | + } |
| 20 | + end |
| 21 | + |
| 22 | + let(:endpoint) { "/api_manage/v1/en/spp_listings/#{spp_listing.id}" } |
| 23 | + |
| 24 | + before do |
| 25 | + ActsAsTenant.current_tenant = website |
| 26 | + end |
| 27 | + |
| 28 | + after do |
| 29 | + ActsAsTenant.current_tenant = nil |
| 30 | + end |
| 31 | + |
| 32 | + # ============================================ |
| 33 | + # Basic Content Updates |
| 34 | + # ============================================ |
| 35 | + describe 'updating translated fields' do |
| 36 | + it 'updates title via Mobility in the specified locale' do |
| 37 | + put endpoint, params: { title: 'Your Dream Mediterranean Retreat' }, headers: auth_headers |
| 38 | + |
| 39 | + expect(response).to have_http_status(:ok) |
| 40 | + json = response.parsed_body |
| 41 | + expect(json['title']).to eq('Your Dream Mediterranean Retreat') |
| 42 | + |
| 43 | + spp_listing.reload |
| 44 | + Mobility.with_locale(:en) do |
| 45 | + expect(spp_listing.title).to eq('Your Dream Mediterranean Retreat') |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + it 'stores translations in the correct locale' do |
| 50 | + put "/api_manage/v1/es/spp_listings/#{spp_listing.id}", |
| 51 | + params: { title: 'Tu Refugio Mediterraneo' }, |
| 52 | + headers: auth_headers |
| 53 | + |
| 54 | + expect(response).to have_http_status(:ok) |
| 55 | + |
| 56 | + spp_listing.reload |
| 57 | + Mobility.with_locale(:es) do |
| 58 | + expect(spp_listing.title).to eq('Tu Refugio Mediterraneo') |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + it 'updates description and SEO fields' do |
| 63 | + put endpoint, params: { |
| 64 | + description: 'Imagine waking up to the sound of waves...', |
| 65 | + seo_title: 'Luxury Biarritz Apartment', |
| 66 | + meta_description: 'Stunning 3-bed apartment in Biarritz...' |
| 67 | + }, headers: auth_headers |
| 68 | + |
| 69 | + expect(response).to have_http_status(:ok) |
| 70 | + json = response.parsed_body |
| 71 | + expect(json['description']).to eq('Imagine waking up to the sound of waves...') |
| 72 | + expect(json['seoTitle']).to eq('Luxury Biarritz Apartment') |
| 73 | + expect(json['metaDescription']).to eq('Stunning 3-bed apartment in Biarritz...') |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + # ============================================ |
| 78 | + # Price Updates |
| 79 | + # ============================================ |
| 80 | + describe 'updating price' do |
| 81 | + it 'updates price_cents and price_currency' do |
| 82 | + put endpoint, params: { price_cents: 550_000_00, price_currency: 'USD' }, headers: auth_headers |
| 83 | + |
| 84 | + expect(response).to have_http_status(:ok) |
| 85 | + json = response.parsed_body |
| 86 | + expect(json['priceCents']).to eq(550_000_00) |
| 87 | + expect(json['priceCurrency']).to eq('USD') |
| 88 | + end |
| 89 | + end |
| 90 | + |
| 91 | + # ============================================ |
| 92 | + # Photo IDs Ordered |
| 93 | + # ============================================ |
| 94 | + describe 'updating photo_ids_ordered' do |
| 95 | + let!(:photo1) { create(:pwb_prop_photo, realty_asset: property, sort_order: 1) } |
| 96 | + let!(:photo2) { create(:pwb_prop_photo, realty_asset: property, sort_order: 2) } |
| 97 | + let!(:photo3) { create(:pwb_prop_photo, realty_asset: property, sort_order: 3) } |
| 98 | + |
| 99 | + it 'accepts valid photo IDs belonging to the same property' do |
| 100 | + put endpoint, params: { photo_ids_ordered: [photo3.id, photo1.id] }, headers: auth_headers |
| 101 | + |
| 102 | + expect(response).to have_http_status(:ok) |
| 103 | + json = response.parsed_body |
| 104 | + expect(json['photoIdsOrdered']).to eq([photo3.id, photo1.id]) |
| 105 | + end |
| 106 | + |
| 107 | + it 'rejects photo IDs from a different property' do |
| 108 | + other_property = create(:pwb_realty_asset, website: website) |
| 109 | + other_photo = create(:pwb_prop_photo, realty_asset: other_property) |
| 110 | + |
| 111 | + put endpoint, params: { photo_ids_ordered: [photo1.id, other_photo.id] }, headers: auth_headers |
| 112 | + |
| 113 | + expect(response).to have_http_status(:unprocessable_entity) |
| 114 | + json = response.parsed_body |
| 115 | + expect(json['error']).to eq('Invalid photo IDs') |
| 116 | + expect(json['message']).to include(other_photo.id.to_s) |
| 117 | + end |
| 118 | + |
| 119 | + it 'accepts an empty array to reset to default order' do |
| 120 | + spp_listing.update!(photo_ids_ordered: [photo2.id, photo1.id]) |
| 121 | + |
| 122 | + put endpoint, params: { photo_ids_ordered: [] }, headers: auth_headers |
| 123 | + |
| 124 | + expect(response).to have_http_status(:ok) |
| 125 | + json = response.parsed_body |
| 126 | + expect(json['photoIdsOrdered']).to eq([]) |
| 127 | + end |
| 128 | + end |
| 129 | + |
| 130 | + # ============================================ |
| 131 | + # Highlighted Features |
| 132 | + # ============================================ |
| 133 | + describe 'updating highlighted_features' do |
| 134 | + it 'accepts an array of feature keys' do |
| 135 | + put endpoint, params: { highlighted_features: %w[sea_views private_pool parking] }, headers: auth_headers |
| 136 | + |
| 137 | + expect(response).to have_http_status(:ok) |
| 138 | + json = response.parsed_body |
| 139 | + expect(json['highlightedFeatures']).to eq(%w[sea_views private_pool parking]) |
| 140 | + end |
| 141 | + end |
| 142 | + |
| 143 | + # ============================================ |
| 144 | + # Template and Settings |
| 145 | + # ============================================ |
| 146 | + describe 'updating template and spp_settings' do |
| 147 | + it 'updates the template' do |
| 148 | + put endpoint, params: { template: 'modern' }, headers: auth_headers |
| 149 | + |
| 150 | + expect(response).to have_http_status(:ok) |
| 151 | + json = response.parsed_body |
| 152 | + expect(json['template']).to eq('modern') |
| 153 | + end |
| 154 | + |
| 155 | + it 'updates spp_settings' do |
| 156 | + put endpoint, params: { spp_settings: { color_scheme: 'dark', layout: 'full-width' } }, headers: auth_headers |
| 157 | + |
| 158 | + expect(response).to have_http_status(:ok) |
| 159 | + json = response.parsed_body |
| 160 | + expect(json['sppSettings']).to eq({ 'color_scheme' => 'dark', 'layout' => 'full-width' }) |
| 161 | + end |
| 162 | + end |
| 163 | + |
| 164 | + # ============================================ |
| 165 | + # Extra Data (Arbitrary JSON) |
| 166 | + # ============================================ |
| 167 | + describe 'updating extra_data' do |
| 168 | + it 'accepts arbitrary JSON' do |
| 169 | + extra = { |
| 170 | + agent_name: 'Marie Dupont', |
| 171 | + agent_phone: '+33 6 12 34 56 78', |
| 172 | + video_tour_url: 'https://youtube.com/watch?v=abc123' |
| 173 | + } |
| 174 | + |
| 175 | + put endpoint, params: { extra_data: extra }, headers: auth_headers |
| 176 | + |
| 177 | + expect(response).to have_http_status(:ok) |
| 178 | + json = response.parsed_body |
| 179 | + expect(json['extraData']['agent_name']).to eq('Marie Dupont') |
| 180 | + expect(json['extraData']['video_tour_url']).to eq('https://youtube.com/watch?v=abc123') |
| 181 | + end |
| 182 | + end |
| 183 | + |
| 184 | + # ============================================ |
| 185 | + # Response Format |
| 186 | + # ============================================ |
| 187 | + describe 'response format' do |
| 188 | + it 'returns the full SppListing JSON' do |
| 189 | + put endpoint, params: { title: 'Updated Title' }, headers: auth_headers |
| 190 | + |
| 191 | + expect(response).to have_http_status(:ok) |
| 192 | + json = response.parsed_body |
| 193 | + expect(json).to include( |
| 194 | + 'id', 'listingType', 'title', 'description', |
| 195 | + 'seoTitle', 'metaDescription', 'priceCents', 'priceCurrency', |
| 196 | + 'photoIdsOrdered', 'highlightedFeatures', 'template', |
| 197 | + 'sppSettings', 'extraData', 'active', 'visible', |
| 198 | + 'liveUrl', 'publishedAt', 'updatedAt' |
| 199 | + ) |
| 200 | + end |
| 201 | + end |
| 202 | + |
| 203 | + # ============================================ |
| 204 | + # Authentication & Authorization |
| 205 | + # ============================================ |
| 206 | + describe 'authentication' do |
| 207 | + it 'returns 401 without authentication' do |
| 208 | + put endpoint, params: { title: 'Test' }, |
| 209 | + headers: { 'HTTP_HOST' => "#{website.subdomain}.localhost" } |
| 210 | + |
| 211 | + expect(response).to have_http_status(:unauthorized) |
| 212 | + end |
| 213 | + end |
| 214 | + |
| 215 | + # ============================================ |
| 216 | + # Tenant Isolation |
| 217 | + # ============================================ |
| 218 | + describe 'tenant isolation' do |
| 219 | + it 'returns 404 for SppListings belonging to other tenants' do |
| 220 | + other_website = create(:pwb_website, subdomain: 'other-tenant') |
| 221 | + other_property = create(:pwb_realty_asset, website: other_website) |
| 222 | + other_listing = create(:pwb_spp_listing, :published, realty_asset: other_property) |
| 223 | + |
| 224 | + put "/api_manage/v1/en/spp_listings/#{other_listing.id}", |
| 225 | + params: { title: 'Hijack attempt' }, |
| 226 | + headers: auth_headers |
| 227 | + |
| 228 | + expect(response).to have_http_status(:not_found) |
| 229 | + end |
| 230 | + end |
| 231 | + |
| 232 | + # ============================================ |
| 233 | + # Not Found |
| 234 | + # ============================================ |
| 235 | + describe 'non-existent listing' do |
| 236 | + it 'returns 404 for unknown IDs' do |
| 237 | + put '/api_manage/v1/en/spp_listings/00000000-0000-0000-0000-000000000000', |
| 238 | + params: { title: 'Test' }, |
| 239 | + headers: auth_headers |
| 240 | + |
| 241 | + expect(response).to have_http_status(:not_found) |
| 242 | + end |
| 243 | + end |
| 244 | +end |
0 commit comments