-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
148 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
api/spec/requests/texts/relationships/collaborators_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
87 changes: 87 additions & 0 deletions
87
api/spec/support/shared_examples/manages_flattened_collaborators.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |