|
259 | 259 | end |
260 | 260 | end |
261 | 261 |
|
| 262 | + # Polymorphic has-one relationship replacing |
| 263 | + describe 'PATCH /tags/:id/relationships/taggable' do |
| 264 | + subject(:last_response) { patch("/tags/#{tag.id}/relationships/taggable", json) } |
| 265 | + |
| 266 | + let!(:old_taggable) { Comment.create } |
| 267 | + let!(:tag) { Tag.create(taggable: old_taggable) } |
| 268 | + let(:policy_scope) { Article.all } |
| 269 | + let(:comment_policy_scope) { Article.all } |
| 270 | + let(:tag_policy_scope) { Tag.all } |
| 271 | + |
| 272 | + before do |
| 273 | + allow_any_instance_of(TagPolicy::Scope).to receive(:resolve).and_return(tag_policy_scope) |
| 274 | + allow_any_instance_of(CommentPolicy::Scope).to receive(:resolve).and_return(comment_policy_scope) |
| 275 | + end |
| 276 | + |
| 277 | + describe 'when replacing with a new taggable' do |
| 278 | + let!(:new_taggable) { Article.create(external_id: 'new-article-id') } |
| 279 | + let(:json) do |
| 280 | + <<-EOS.strip_heredoc |
| 281 | + { |
| 282 | + "data": { |
| 283 | + "type": "articles", |
| 284 | + "id": "#{new_taggable.external_id}" |
| 285 | + } |
| 286 | + } |
| 287 | + EOS |
| 288 | + end |
| 289 | + |
| 290 | + context 'unauthorized for replace_to_one_relationship' do |
| 291 | + before { disallow_operation('replace_to_one_relationship', tag, new_taggable, :taggable) } |
| 292 | + it { is_expected.to be_forbidden } |
| 293 | + end |
| 294 | + |
| 295 | + context 'authorized for replace_to_one_relationship' do |
| 296 | + before { allow_operation('replace_to_one_relationship', tag, new_taggable, :taggable) } |
| 297 | + it { is_expected.to be_successful } |
| 298 | + |
| 299 | + context 'limited by policy scope on taggable', skip: 'DISCUSS' do |
| 300 | + let(:policy_scope) { Article.where.not(id: tag.taggable.id) } |
| 301 | + it { is_expected.to be_not_found } |
| 302 | + end |
| 303 | + |
| 304 | + # If this happens in real life, it's mostly a bug. We want to document the |
| 305 | + # behaviour in that case anyway, as it might be surprising. |
| 306 | + context 'limited by policy scope on tag' do |
| 307 | + let(:tag_policy_scope) { Tag.where.not(id: tag.id) } |
| 308 | + it { is_expected.to be_not_found } |
| 309 | + end |
| 310 | + end |
| 311 | + end |
| 312 | + |
| 313 | + # https://github.com/cerebris/jsonapi-resources/issues/1081 |
| 314 | + describe 'when nullifying the taggable', skip: 'Broken upstream' do |
| 315 | + let(:new_taggable) { nil } |
| 316 | + let(:json) { '{ "data": null }' } |
| 317 | + |
| 318 | + context 'unauthorized for remove_to_one_relationship' do |
| 319 | + before { disallow_operation('remove_to_one_relationship', tag, :taggable) } |
| 320 | + it { is_expected.to be_forbidden } |
| 321 | + end |
| 322 | + |
| 323 | + context 'authorized for remove_to_one_relationship' do |
| 324 | + before { allow_operation('remove_to_one_relationship', tag, :taggable) } |
| 325 | + it { is_expected.to be_successful } |
| 326 | + |
| 327 | + context 'limited by policy scope on taggable', skip: 'DISCUSS' do |
| 328 | + let(:policy_scope) { Article.where.not(id: tag.taggable.id) } |
| 329 | + it { is_expected.to be_not_found } |
| 330 | + end |
| 331 | + |
| 332 | + # If this happens in real life, it's mostly a bug. We want to document the |
| 333 | + # behaviour in that case anyway, as it might be surprising. |
| 334 | + context 'limited by policy scope on tag' do |
| 335 | + let(:tag_policy_scope) { Tag.where.not(id: tag.id) } |
| 336 | + it { is_expected.to be_not_found } |
| 337 | + end |
| 338 | + end |
| 339 | + end |
| 340 | + end |
| 341 | + |
262 | 342 | describe 'DELETE /articles/:id/relationships/comments' do |
263 | 343 | let(:article) { articles(:article_with_comments) } |
264 | 344 | let(:comments_to_remove) { article.comments.limit(2) } |
|
0 commit comments