2020-04-30 15:05:41 -07:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "rails_helper"
|
|
|
|
|
2020-05-25 09:04:54 -07:00
|
|
|
describe Ajax::ListController, :ajax_controller, type: :controller do
|
2020-04-30 15:05:41 -07:00
|
|
|
let(:target_user) { FactoryBot.create(:user) }
|
|
|
|
|
|
|
|
describe "#create" do
|
|
|
|
let(:name) { "I signori della gallassia" }
|
|
|
|
let(:target_user_param) { target_user.screen_name }
|
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
"name" => name,
|
|
|
|
"user" => target_user_param
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
subject { post(:create, params: params) }
|
|
|
|
|
|
|
|
context "when user is signed in" do
|
|
|
|
let(:expected_response) do
|
|
|
|
{
|
|
|
|
"success" => true,
|
2022-07-09 10:16:18 -07:00
|
|
|
"status" => "okay",
|
2020-04-30 15:05:41 -07:00
|
|
|
"message" => anything,
|
2022-07-09 10:16:18 -07:00
|
|
|
"render" => anything
|
2020-04-30 15:05:41 -07:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
before(:each) { sign_in(user) }
|
|
|
|
|
2020-05-25 09:04:54 -07:00
|
|
|
it "creates the list" do
|
|
|
|
expect { subject }.to(change { user.lists.count }.by(1))
|
2020-04-30 15:05:41 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
include_examples "returns the expected response"
|
|
|
|
|
|
|
|
context "when name param is missing" do
|
|
|
|
let(:name) { "" }
|
|
|
|
let(:expected_response) do
|
|
|
|
{
|
|
|
|
"success" => false,
|
2022-07-09 10:16:18 -07:00
|
|
|
"status" => "parameter_error",
|
2020-04-30 15:05:41 -07:00
|
|
|
"message" => anything
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2020-05-25 09:04:54 -07:00
|
|
|
it "does not create the list" do
|
|
|
|
expect { subject }.not_to(change { user.lists.count })
|
2020-04-30 15:05:41 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
include_examples "returns the expected response"
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when target user does not exist" do
|
|
|
|
let(:target_user_param) { "giuseppe-drogo" }
|
|
|
|
let(:expected_response) do
|
|
|
|
{
|
|
|
|
"success" => false,
|
2022-07-09 10:16:18 -07:00
|
|
|
"status" => "not_found",
|
2020-04-30 15:05:41 -07:00
|
|
|
"message" => anything
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2020-05-25 09:04:54 -07:00
|
|
|
it "does not create the list" do
|
|
|
|
expect { subject }.not_to(change { user.lists.count })
|
2020-04-30 15:05:41 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
include_examples "returns the expected response"
|
|
|
|
end
|
|
|
|
|
2020-05-25 09:04:54 -07:00
|
|
|
context "when list name is invalid for reasons" do
|
2020-04-30 15:05:41 -07:00
|
|
|
let(:name) { "\u{1f43e}" }
|
|
|
|
let(:expected_response) do
|
|
|
|
{
|
|
|
|
"success" => false,
|
2022-07-09 10:16:18 -07:00
|
|
|
"status" => "err",
|
2020-04-30 15:05:41 -07:00
|
|
|
"message" => anything
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2020-05-25 09:04:54 -07:00
|
|
|
it "does not create the list" do
|
|
|
|
expect { subject }.not_to(change { user.lists.count })
|
2020-04-30 15:05:41 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
include_examples "returns the expected response"
|
|
|
|
end
|
|
|
|
|
2020-05-25 09:04:54 -07:00
|
|
|
context "when list already exists" do
|
2020-04-30 15:05:41 -07:00
|
|
|
before(:each) { post(:create, params: params) }
|
|
|
|
let(:expected_response) do
|
|
|
|
{
|
|
|
|
"success" => false,
|
2022-07-09 10:16:18 -07:00
|
|
|
"status" => "err",
|
2020-04-30 15:05:41 -07:00
|
|
|
"message" => anything
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2020-05-25 09:04:54 -07:00
|
|
|
it "does not create the list" do
|
|
|
|
expect { subject }.not_to(change { user.lists.count })
|
2020-04-30 15:05:41 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
include_examples "returns the expected response"
|
|
|
|
end
|
|
|
|
|
2020-05-25 09:04:54 -07:00
|
|
|
context "when someone else created a list with the same name" do
|
2020-04-30 15:05:41 -07:00
|
|
|
before(:each) do
|
2020-05-25 09:04:54 -07:00
|
|
|
FactoryBot.create(:list, user: target_user, display_name: name)
|
2020-04-30 15:05:41 -07:00
|
|
|
end
|
|
|
|
|
2020-05-25 09:04:54 -07:00
|
|
|
it "creates the list" do
|
|
|
|
expect { subject }.to(change { user.lists.count }.by(1))
|
2020-04-30 15:05:41 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
include_examples "returns the expected response"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when user is not signed in" do
|
2022-07-09 10:05:35 -07:00
|
|
|
it_behaves_like "requires login"
|
2020-04-30 15:05:41 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#destroy" do
|
|
|
|
let(:name) { "I signori della gallassia" }
|
2020-05-25 09:04:54 -07:00
|
|
|
let(:list) { FactoryBot.create(:list, user: user, display_name: name) }
|
|
|
|
let(:list_param) { list.name }
|
2020-04-30 15:05:41 -07:00
|
|
|
let(:params) do
|
|
|
|
{
|
2020-05-25 09:04:54 -07:00
|
|
|
"list" => list_param
|
2020-04-30 15:05:41 -07:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
subject { delete(:destroy, params: params) }
|
|
|
|
|
|
|
|
context "when user is signed in" do
|
|
|
|
let(:expected_response) do
|
|
|
|
{
|
|
|
|
"success" => true,
|
2022-07-09 10:16:18 -07:00
|
|
|
"status" => "okay",
|
2020-04-30 15:05:41 -07:00
|
|
|
"message" => anything
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
before(:each) { sign_in(user) }
|
|
|
|
|
2020-05-25 09:04:54 -07:00
|
|
|
it "deletes the list" do
|
|
|
|
list
|
|
|
|
expect { subject }.to(change { user.lists.count }.by(-1))
|
2020-04-30 15:05:41 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
include_examples "returns the expected response"
|
|
|
|
|
2020-05-25 09:04:54 -07:00
|
|
|
context "when list param is missing" do
|
|
|
|
let(:list_param) { "" }
|
2020-04-30 15:05:41 -07:00
|
|
|
let(:expected_response) do
|
|
|
|
{
|
|
|
|
"success" => false,
|
2022-07-09 10:16:18 -07:00
|
|
|
"status" => "parameter_error",
|
2020-04-30 15:05:41 -07:00
|
|
|
"message" => anything
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2020-05-25 09:04:54 -07:00
|
|
|
it "does not delete the list" do
|
|
|
|
expect { subject }.not_to(change { user.lists.count })
|
2020-04-30 15:05:41 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
include_examples "returns the expected response"
|
|
|
|
end
|
|
|
|
|
2020-05-25 09:04:54 -07:00
|
|
|
context "when list does not exist" do
|
|
|
|
let(:list_param) { "the-foobars-and-the-dingdongs" }
|
2020-04-30 15:05:41 -07:00
|
|
|
let(:expected_response) do
|
|
|
|
{
|
|
|
|
"success" => false,
|
2022-07-09 10:16:18 -07:00
|
|
|
"status" => "err",
|
2020-04-30 15:05:41 -07:00
|
|
|
"message" => anything
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2020-05-25 09:04:54 -07:00
|
|
|
it "does not delete the list" do
|
|
|
|
expect { subject }.not_to(change { user.lists.count })
|
2020-04-30 15:05:41 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
include_examples "returns the expected response"
|
|
|
|
end
|
|
|
|
|
2020-05-25 09:04:54 -07:00
|
|
|
context "when someone else created a list with the same name" do
|
2020-04-30 15:05:41 -07:00
|
|
|
before(:each) do
|
2020-05-25 09:04:54 -07:00
|
|
|
list
|
|
|
|
FactoryBot.create(:list, user: target_user, display_name: name)
|
2020-04-30 15:05:41 -07:00
|
|
|
end
|
|
|
|
|
2020-05-25 09:04:54 -07:00
|
|
|
it "deletes the list" do
|
|
|
|
expect { subject }.to(change { user.lists.count }.by(-1))
|
2020-04-30 15:05:41 -07:00
|
|
|
end
|
|
|
|
|
2020-05-25 09:04:54 -07:00
|
|
|
it "does not delete the other users' list" do
|
|
|
|
expect { subject }.not_to(change { target_user.lists.count })
|
2020-04-30 15:05:41 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
include_examples "returns the expected response"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when user is not signed in" do
|
2022-07-09 10:05:35 -07:00
|
|
|
it_behaves_like "requires login"
|
2020-04-30 15:05:41 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#membership" do
|
|
|
|
let(:name) { "The Agency" }
|
|
|
|
let(:members) { [] }
|
2020-05-25 09:04:54 -07:00
|
|
|
let(:list) { FactoryBot.create(:list, user: user, display_name: name, members: members) }
|
|
|
|
let(:list_param) { list.name }
|
2020-04-30 15:05:41 -07:00
|
|
|
let(:target_user_param) { target_user.screen_name }
|
|
|
|
let(:params) do
|
|
|
|
{
|
2020-05-25 09:04:54 -07:00
|
|
|
"list" => list_param,
|
2020-04-30 15:05:41 -07:00
|
|
|
"user" => target_user_param,
|
2022-07-09 10:16:18 -07:00
|
|
|
"add" => add_param
|
2020-04-30 15:05:41 -07:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
subject { post(:membership, params: params) }
|
|
|
|
|
|
|
|
context "when user is signed in" do
|
|
|
|
let(:expected_response) do
|
|
|
|
{
|
|
|
|
"success" => true,
|
2022-07-09 10:16:18 -07:00
|
|
|
"status" => "okay",
|
2020-04-30 15:05:41 -07:00
|
|
|
"message" => anything,
|
|
|
|
"checked" => expected_checked
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
before(:each) { sign_in(user) }
|
|
|
|
|
|
|
|
context "when add is false" do
|
|
|
|
let(:add_param) { "false" }
|
|
|
|
let(:expected_checked) { false }
|
|
|
|
|
|
|
|
it "does not do anything" do
|
2020-05-25 09:04:54 -07:00
|
|
|
expect { subject }.not_to(change { list.members })
|
2022-07-09 10:16:18 -07:00
|
|
|
expect(list.members.map { |gm| gm.user.id }.sort).to eq([])
|
2020-04-30 15:05:41 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
include_examples "returns the expected response"
|
|
|
|
|
2020-05-25 09:04:54 -07:00
|
|
|
context "when the user was already added to the list" do
|
2020-04-30 15:05:41 -07:00
|
|
|
let(:members) { [target_user] }
|
|
|
|
|
2020-05-25 09:04:54 -07:00
|
|
|
it "removes the user from the list" do
|
|
|
|
expect { subject }.to(change { list.reload.members.map { |gm| gm.user.id }.sort }.from([target_user.id]).to([]))
|
2020-04-30 15:05:41 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
include_examples "returns the expected response"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when add is true" do
|
|
|
|
let(:add_param) { "true" }
|
|
|
|
let(:expected_checked) { true }
|
|
|
|
|
2020-05-25 09:04:54 -07:00
|
|
|
it "adds the user to the list" do
|
|
|
|
expect { subject }.to(change { list.reload.members.map { |gm| gm.user.id }.sort }.from([]).to([target_user.id]))
|
2020-04-30 15:05:41 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
include_examples "returns the expected response"
|
|
|
|
|
2020-05-25 09:04:54 -07:00
|
|
|
context "when the user was already added to the list" do
|
2020-04-30 15:05:41 -07:00
|
|
|
let(:members) { [target_user] }
|
|
|
|
|
2020-05-25 09:04:54 -07:00
|
|
|
it "does not add the user to the list again" do
|
|
|
|
expect { subject }.not_to(change { list.members })
|
2022-07-09 10:16:18 -07:00
|
|
|
expect(list.members.map { |gm| gm.user.id }.sort).to eq([target_user.id])
|
2020-04-30 15:05:41 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
include_examples "returns the expected response"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-25 09:04:54 -07:00
|
|
|
context "when list does not exist" do
|
|
|
|
let(:list_param) { "the-good-agency" }
|
2020-04-30 15:05:41 -07:00
|
|
|
let(:add_param) { "add" }
|
|
|
|
let(:expected_response) do
|
|
|
|
{
|
|
|
|
"success" => false,
|
2022-07-09 10:16:18 -07:00
|
|
|
"status" => "not_found",
|
2020-04-30 15:05:41 -07:00
|
|
|
"message" => anything
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
include_examples "returns the expected response"
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when target user does not exist" do
|
|
|
|
let(:target_user_param) { "erwin-proell" }
|
|
|
|
let(:add_param) { "add" }
|
|
|
|
let(:expected_response) do
|
|
|
|
{
|
|
|
|
"success" => false,
|
2022-07-09 10:16:18 -07:00
|
|
|
"status" => "not_found",
|
2020-04-30 15:05:41 -07:00
|
|
|
"message" => anything
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
include_examples "returns the expected response"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-06-12 14:35:20 -07:00
|
|
|
context "when the target user is blocking the current user" do
|
|
|
|
let(:members) { [user] }
|
|
|
|
let(:add_param) { "add" }
|
|
|
|
let(:expected_response) do
|
|
|
|
{
|
|
|
|
"success" => false,
|
|
|
|
"status" => "other_blocking_self",
|
2022-06-12 14:36:44 -07:00
|
|
|
"message" => anything
|
2022-06-12 14:35:20 -07:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not add the user to the list" do
|
|
|
|
expect { subject }.not_to(change { list.members })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the user being added is blocking the current user" do
|
|
|
|
let(:members) { [user] }
|
|
|
|
let(:add_param) { "add" }
|
|
|
|
let(:expected_response) do
|
|
|
|
{
|
|
|
|
"success" => false,
|
|
|
|
"status" => "self_blocking_other",
|
2022-06-12 14:36:44 -07:00
|
|
|
"message" => anything
|
2022-06-12 14:35:20 -07:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not add the user to the list" do
|
|
|
|
expect { subject }.not_to(change { list.members })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-30 15:05:41 -07:00
|
|
|
context "when user is not signed in" do
|
|
|
|
let(:add_param) { "whatever" }
|
|
|
|
|
2022-07-09 10:05:35 -07:00
|
|
|
it_behaves_like "requires login"
|
2020-04-30 15:05:41 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|