docbrown/spec/requests/admin/organization_memberships_spec.rb
rhymes 7ff882b8ce
Refactor Admin Member Detail view - Tools section (#14283)
* Test with Grid layout

* Use Flexbox

* Replace with utility classes

* Wire up Tools -> Emails

* Stash: will rebase with a better commit message

* Fix transition between Email and Tools component

* Refactor Verify Email Ownership button a bit

* Use respond_to for verify_email_ownership

* Wrap the Preact Snackbar controller in Stimulus and use it from users/tools/email_controller

* Add HTML5 validation to EmailComponent

* Validation and cleanup

* Add Email history list and fix styling

* Additional styling cleanups

* Add error handling

* Close panel after email operations

* Actually use <local-time> GitHub time element correctly

* Add specs for Tools component and controller

* Email to Emails

* Add tests for Admin::Users::Tools::EmailsComponent

* Fix bug with ToolsComponent instantiation in ToolsController

* Add notes to show page

* Add ToolsComponent css

* Use Rails UJS instead of manual Stimulus to connect remote helpers

* Make Notes section come alive by adding its code

* Make Credits section come alive by adding its code

* Go back to vertical flex

* Finalize small restructuring of credits code

* Simplify ToolsComponent instantiation

* Add basic Add user to org functionality

* Make update user permissions form work

* Make remove user from org work

* Use generic Stimulus AjaxController to cleanup code

* Use Stimulus AjaxController for NotesComponent

* Use Stimulus AjaxController for CreditsComponent

* Use Stimulus AjaxController for OrganizationsController

* Add Admin::Users::Tools::ReportsComponent

* Do not display snackbar message if there is no message

* Add Admin::Users::Tools::ReactionsComponent

* Fix EmailsComponent spec

* Add CreditsComponent tests

* Fix quotes

* Add OrganizationsComponent specs

* Add ReportsComponent spec

* Add ReactionsComponent spec

* Fix rubocop violation

* Fix ToolsComponent specs

* Remove unused variable

* More tests

* Use keyword argument for ToolsComponent

* Fill in Tools requests specs

* Use Rspec shared_examples for ToolsController and EmailsController

* Add tests for Admin::Users::Tools::CreditsController

* Add tests for Admin::Users::Tools::NotesController

* Add tests for Admin::Users::Tools::OrganizationsController

* Add tests for Admin::Users::Tools::ReactionsController and ReportsController

* Fix bugs and add tests to Admin::OrganizationMembershipsController

* Add comments to deprecated sections of the UsersController

* Fix bugs and add tests to Admin::UsersController #send_email and #verify_email_ownership

* Add User model tests

* Feature flag fixes

* Add Cypress Tools - Emails tests

* Add Cypress Tools - Notes tests

* Add Cypress Tools - Credits tests

* Add Cypress Tools - Organizations tests

* Add Cypress Tools - Reports and Reactions tests

* Mark the replace target as a polite region

* Update view_component gem

* Tiny fixes

* Fix spec

* Wrap component rendering in render_component

* Move user.related_negative_reactions to a Reaction scope

* Move user.reports to a FeedbackMessage scope

* Move user.last_verification_date as EmailAuthorization class method

* Revert encapsulation to private

* Fix boxes backlinks names

* Add keyboard focus styling to boxes

* Remove duplicate styling

* Remove duplicated header element

* Improve heading hiearchy

* Fix <legend> and labels

* Backlink should be Tools not Users

* Announce section change to screen reader and fix focus

* Fix specs

* Add focus style for backlinks

* Enable email sending in e2e mode

* Use Settings instead of env variable
2021-08-17 18:55:53 +02:00

255 lines
8.6 KiB
Ruby

require "rails_helper"
RSpec.describe "/admin/organization_memberships", type: :request do
let(:admin) { create(:user, :super_admin) }
let(:user) { create(:user) }
let(:organization) { create(:organization) }
before do
sign_in(admin)
end
describe "#create" do
context "when interacting via a browser" do
it "errors if a param is missing", :aggregate_failures do
expect do
post admin_organization_memberships_path, params: {
organization_membership: {
user_id: user.id,
type_of_user: :a
}
}
end.to not_change(user.organizations, :count)
expect(response).to redirect_to(admin_user_path(user.id))
expect(flash[:danger]).to include("does not exist")
end
it "errors if a param is invalid", :aggregate_failures do
expect do
post admin_organization_memberships_path, params: {
organization_membership: {
user_id: user.id,
type_of_user: :a,
organization_id: organization.id
}
}
end.to not_change(user.organizations, :count)
expect(response).to redirect_to(admin_user_path(user.id))
expect(flash[:danger]).to include("not included in the list")
end
it "adds a user to an organization", :aggregate_failures do
expect do
post admin_organization_memberships_path, params: {
organization_membership: {
user_id: user.id,
type_of_user: :member,
organization_id: organization.id
}
}
end.to change(user.organizations, :count).by(1)
expect(response).to redirect_to(admin_user_path(user.id))
expect(flash[:success]).to include("successfully added")
end
end
context "when interacting via ajax" do
it "returns :unprocessable_entity if a param is missing", :aggregate_failures do
expect do
post admin_organization_memberships_path, params: {
organization_membership: {
user_id: user.id,
type_of_user: :a
}
}, xhr: true
end.to not_change(user.organizations, :count)
expect(response).to have_http_status(:unprocessable_entity)
expect(response.parsed_body["error"]).to include("does not exist")
end
it "returns :unprocessable_entity if a param is invalid", :aggregate_failures do
expect do
post admin_organization_memberships_path, params: {
organization_membership: {
user_id: user.id,
type_of_user: :a,
organization_id: organization.id
}
}, xhr: true
end.to not_change(user.organizations, :count)
expect(response).to have_http_status(:unprocessable_entity)
expect(response.parsed_body["error"]).to include("not included in the list")
end
it "adds a user to an organization", :aggregate_failures do
expect do
post admin_organization_memberships_path, params: {
organization_membership: {
user_id: user.id,
type_of_user: :member,
organization_id: organization.id
}
}, xhr: true
end.to change(user.organizations, :count).by(1)
expect(response).to have_http_status(:created)
expect(response.parsed_body["result"]).to include("successfully added")
end
end
end
describe "#update" do
let(:membership) { create(:organization_membership, user: user, organization: organization, type_of_user: :member) }
it "returns not found for non existing memberships" do
expect do
put admin_organization_membership_path(9999), params: {
organization_membership: {
type_of_user: :member
}
}
end.to raise_error(ActiveRecord::RecordNotFound)
end
context "when interacting via a browser" do
it "errors if a param is invalid", :aggregate_failures do
expect do
put admin_organization_membership_path(membership.id), params: {
organization_membership: {
type_of_user: :a
}
}
end.to not_change(membership, :type_of_user)
expect(response).to redirect_to(admin_user_path(user.id))
expect(flash[:danger]).to include("not included in the list")
end
it "cannot change the user id", :aggregate_failures do
put admin_organization_membership_path(membership.id), params: {
organization_membership: {
type_of_user: :admin,
user_id: create(:user).id
}
}
expect(membership.reload.user_id).to eq(user.id)
end
it "cannot change the organization id", :aggregate_failures do
put admin_organization_membership_path(membership.id), params: {
organization_membership: {
type_of_user: :admin,
organization_id: create(:organization).id
}
}
expect(membership.reload.organization_id).to eq(organization.id)
end
it "changes the membership type of user", :aggregate_failures do
expect do
put admin_organization_membership_path(membership.id), params: {
organization_membership: {
type_of_user: :admin
}
}
end.to change(user.organizations, :count).by(1)
expect(membership.reload.organization_id).to eq(organization.id)
expect(response).to redirect_to(admin_user_path(user.id))
expect(flash[:success]).to include("successfully updated")
end
end
context "when interacting via ajax" do
it "errors if a param is invalid", :aggregate_failures do
expect do
put admin_organization_membership_path(membership.id), params: {
organization_membership: {
type_of_user: :a
}
}, xhr: true
end.to not_change(membership, :type_of_user)
expect(response).to have_http_status(:unprocessable_entity)
expect(response.parsed_body["error"]).to include("not included in the list")
end
it "cannot change the user id", :aggregate_failures do
put admin_organization_membership_path(membership.id), params: {
organization_membership: {
type_of_user: :admin,
user_id: create(:user).id
}
}, xhr: true
expect(membership.reload.user_id).to eq(user.id)
end
it "cannot change the organization id", :aggregate_failures do
put admin_organization_membership_path(membership.id), params: {
organization_membership: {
type_of_user: :admin,
organization_id: create(:organization).id
}
}, xhr: true
expect(membership.reload.organization_id).to eq(organization.id)
end
it "changes the membership type of user", :aggregate_failures do
expect do
put admin_organization_membership_path(membership.id), params: {
organization_membership: {
type_of_user: :admin
}
}, xhr: true
end.to change(user.organizations, :count).by(1)
expect(membership.reload.organization_id).to eq(organization.id)
expect(response).to have_http_status(:ok)
expect(response.parsed_body["result"]).to include("successfully updated")
end
end
end
describe "#destroy" do
it "returns not found for non existing memberships" do
expect do
delete admin_organization_membership_path(9999)
end.to raise_error(ActiveRecord::RecordNotFound)
end
context "when interacting via a browser" do
it "removes the membership of the user from an org", :aggregate_failures do
membership = create(:organization_membership, user: user, organization: organization)
expect do
delete admin_organization_membership_path(membership.id)
end.to change(user.organizations, :count).by(-1)
expect(response).to redirect_to(admin_user_path(user.id))
expect(flash[:success]).to include("successfully removed")
end
end
context "when interacting via ajax" do
it "removes the membership of the user from an org", :aggregate_failures do
membership = create(:organization_membership, user: user, organization: organization)
expect do
delete admin_organization_membership_path(membership.id), xhr: true
end.to change(user.organizations, :count).by(-1)
expect(response).to have_http_status(:ok)
expect(response.parsed_body["result"]).to include("successfully removed")
end
end
end
end