Skip to content

Commit

Permalink
[C] Add specs
Browse files Browse the repository at this point in the history
  • Loading branch information
1aurend committed Feb 3, 2025
1 parent 556280f commit 6638dad
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Relationships
class CollaboratorsController < ApplicationController
include API::V1::ManagesFlattenedCollaborators

authority_actions create_from_roles: "create"
authority_actions create_from_roles: :create

before_action :set_text

Expand Down
28 changes: 28 additions & 0 deletions api/spec/requests/api/v1/collaborators_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
require "swagger_helper"

RSpec.describe "Collaborators", type: :request do
context "GET /api/v1/collaborators/roles" do
it "returns a 200 response" do
get roles_api_v1_collaborators_path
expect(response).to have_http_status :ok
end
end

context "when relating to a project" do
let(:resource) { FactoryBot.create(:collaborator) }
let(:project_id) { resource.collaboratable_id }
Expand All @@ -20,4 +27,25 @@
url_parameters: [:project_id]
end
end

context "when relating to a text" do
let(:text) { FactoryBot.create(:text) }
let(:resource) { FactoryBot.create(:collaborator, collaboratable: text) }
let(:text_id) { text.id }

path "/texts/{text_id}/relationships/collaborators" do
include_examples "an API index request",
parent: "text",
model: Collaborator,
url_parameters: [:text_id],
included_relationships: [:maker]
end

path "/texts/{text_id}/relationships/collaborators/{id}" do
include_examples "an API show request",
parent: "text",
model: Collaborator,
url_parameters: [:text_id]
end
end
end
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require "rails_helper"

RSpec.describe "Project Collaborators API", type: :request do
it_behaves_like "a controller handling flattened collaborators", :project

let(:project) { FactoryBot.create(:project) }
let(:maker) { FactoryBot.create(:maker) }
Expand Down
31 changes: 31 additions & 0 deletions api/spec/requests/texts/relationships/collaborators_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require "rails_helper"

RSpec.describe "Text Collaborators API", type: :request do
it_behaves_like "a controller handling flattened collaborators", :text

let(:text) { FactoryBot.create(:text) }
let(:maker) { FactoryBot.create(:maker) }
let(:collaborator) {
FactoryBot.create(:collaborator, maker: maker, collaboratable: text)
}

describe "sends text collaborators" do
let(:path) { api_v1_text_relationships_collaborators_path(text) }
before(:each) { get path }
describe "the response" do
it "has a 200 status code" do
expect(response).to have_http_status :ok
end
end
end

describe "sends a text collaborator" do
let(:path) { api_v1_text_relationships_collaborator_path(text, collaborator) }
before(:each) { get path }
describe "the response" do
it "has a 200 status code" do
expect(response).to have_http_status :ok
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
require "rails_helper"

shared_examples_for "a controller handling flattened collaborators" do | factory |
let_it_be(:maker_one) { FactoryBot.create(:maker) }
let_it_be(:maker_two) { FactoryBot.create(:maker) }
let_it_be(:object) { FactoryBot.create(factory) }

describe "creates users from a list of roles" do
let(:params) do
{
data: {
roles: [ "author", "editor" ],
maker: { data: maker_one }
}
}
end
let(:path) do
url_for([:create_from_roles, :api, :v1, object, :relationships, :collaborators])
end

context "when the user is a project_creator" do
let(:headers) { project_creator_headers }

it "creates one collaborator per role" do
expect do
post path, headers: headers, params: params.to_json
end.to change(object.collaborators, :count).by(2)
end
end

context "when there is no authenticated user" do
let(:headers) { anonymous_headers }

it "does not create collaborators" do
expect do
post path, headers: headers, params: params.to_json
end.to keep_the_same { object.collaborators.count }
end
end
end

describe "deletes all collaborators associated with a maker" do
let_it_be(:collaborator_one) {
FactoryBot.create(:collaborator, maker: maker_one, collaboratable: object, role: :author)
}
let_it_be(:collaborator_two) {
FactoryBot.create(:collaborator, maker: maker_one, collaboratable: object, role: :editor)
}
let_it_be(:collaborator_three) {
FactoryBot.create(:collaborator, maker: maker_two, collaboratable: object, role: :editor)
}

let(:filter) { { maker: maker_one.id } }
let(:params) { { filter: filter } }
let(:path) do
url_for([:api, :v1, object, :relationships, :collaborators])
end

context "when the user is a project_creator" do
let(:headers) { project_creator_headers }

it "deletes collaborator records when a maker id is supplied" do
expect do
delete path, headers: headers, params: params.to_json
end.to change(object.collaborators, :count).by(-2)
.and change(object.collaborators.where(maker_id: maker_one.id), :count).to(0)
end

it "does not delete records when no maker id is provided" do
expect do
delete path, headers: headers
end.to keep_the_same { object.collaborators.count }
end
end

context "when there is no authenticated user" do
let(:headers) { anonymous_headers }

it "does not delete collaborators" do
expect do
delete path, headers: headers, params: params.to_json
end.to keep_the_same { object.collaborators.count }
.and keep_the_same { object.collaborators.where(maker_id: maker_one.id).count }
end
end
end
end

0 comments on commit 6638dad

Please sign in to comment.