2020-04-19 14:40:06 -07:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "rails_helper"
|
|
|
|
|
|
|
|
describe AnnouncementController, type: :controller do
|
2020-04-19 14:59:57 -07:00
|
|
|
let(:user) { FactoryBot.create(:user, roles: [:administrator]) }
|
2020-04-19 14:40:06 -07:00
|
|
|
|
|
|
|
describe "#index" do
|
|
|
|
subject { get :index }
|
|
|
|
|
|
|
|
context "user signed in" do
|
|
|
|
before(:each) { sign_in(user) }
|
|
|
|
|
|
|
|
it "renders the index template" do
|
|
|
|
subject
|
|
|
|
expect(response).to render_template(:index)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "no announcements" do
|
|
|
|
it "@announcements is empty" do
|
|
|
|
subject
|
|
|
|
expect(assigns(:announcements)).to be_blank
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "one announcement" do
|
2022-01-25 13:09:07 -08:00
|
|
|
let!(:announcement) { Announcement.create(content: "I am announcement", user: user, starts_at: Time.current, ends_at: 2.days.from_now) }
|
2020-04-19 14:40:06 -07:00
|
|
|
|
|
|
|
it "includes the announcement in the @announcements assign" do
|
|
|
|
subject
|
|
|
|
expect(assigns(:announcements)).to include(announcement)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#new" do
|
|
|
|
subject { get :new }
|
|
|
|
|
|
|
|
context "user signed in" do
|
|
|
|
before(:each) { sign_in(user) }
|
|
|
|
|
|
|
|
it "renders the new template" do
|
|
|
|
subject
|
|
|
|
expect(response).to render_template(:new)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#create" do
|
|
|
|
let :announcement_params do
|
|
|
|
{
|
|
|
|
announcement: {
|
2022-01-25 13:09:07 -08:00
|
|
|
content: "I like dogs!",
|
2020-04-19 14:40:06 -07:00
|
|
|
starts_at: Time.current,
|
2022-01-25 13:09:07 -08:00
|
|
|
ends_at: 2.days.from_now
|
2020-04-19 14:40:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
subject { post :create, params: announcement_params }
|
|
|
|
|
|
|
|
context "user signed in" do
|
|
|
|
before(:each) { sign_in(user) }
|
|
|
|
|
|
|
|
it "creates an announcement" do
|
|
|
|
expect { subject }.to change { Announcement.count }.by(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "redirects to announcement#index" do
|
|
|
|
subject
|
|
|
|
expect(response).to redirect_to(:announcement_index)
|
|
|
|
end
|
|
|
|
end
|
2022-01-25 13:08:10 -08:00
|
|
|
|
|
|
|
context "submitting a malformed announcement" do
|
|
|
|
before(:each) { sign_in(user) }
|
|
|
|
|
|
|
|
let :announcement_params do
|
|
|
|
{
|
|
|
|
announcement: {
|
2022-01-25 13:09:07 -08:00
|
|
|
content: "I like dogs!",
|
2022-01-25 13:08:10 -08:00
|
|
|
starts_at: Time.current,
|
2022-01-25 13:09:07 -08:00
|
|
|
ends_at: 2.days.ago
|
2022-01-25 13:08:10 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it "stays in the new view when a malformed announcement is submitted" do
|
|
|
|
post :create, params: announcement_params
|
|
|
|
expect(response).to render_template(:new)
|
|
|
|
end
|
|
|
|
end
|
2020-04-19 14:40:06 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "#edit" do
|
|
|
|
let! :announcement do
|
2022-01-25 13:09:07 -08:00
|
|
|
Announcement.create(content: "Dogs are pretty cool, I guess",
|
|
|
|
starts_at: 3.days.from_now,
|
|
|
|
ends_at: 10.days.from_now,
|
|
|
|
user: user)
|
2020-04-19 14:40:06 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
subject { get :edit, params: { id: announcement.id } }
|
|
|
|
|
|
|
|
context "user signed in" do
|
|
|
|
before(:each) { sign_in(user) }
|
|
|
|
|
|
|
|
it "renders the edit template" do
|
|
|
|
subject
|
|
|
|
expect(response).to render_template(:edit)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#update" do
|
|
|
|
let :announcement_params do
|
|
|
|
{
|
|
|
|
content: "The trebuchet is the superior siege weapon"
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
let! :announcement do
|
2022-01-25 13:09:07 -08:00
|
|
|
Announcement.create(content: "Dogs are pretty cool, I guess",
|
|
|
|
starts_at: 3.days.from_now,
|
|
|
|
ends_at: 10.days.from_now,
|
|
|
|
user: user)
|
2020-04-19 14:40:06 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
subject do
|
|
|
|
patch :update, params: {
|
2022-01-25 13:09:07 -08:00
|
|
|
id: announcement.id,
|
2020-04-19 14:40:06 -07:00
|
|
|
announcement: announcement_params
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
context "user signed in" do
|
|
|
|
before(:each) { sign_in(user) }
|
|
|
|
|
|
|
|
it "updates the announcement" do
|
|
|
|
subject
|
|
|
|
updated = Announcement.find announcement.id
|
|
|
|
expect(updated.content).to eq(announcement_params[:content])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "redirects to announcement#index" do
|
|
|
|
subject
|
|
|
|
expect(response).to redirect_to(:announcement_index)
|
|
|
|
end
|
|
|
|
end
|
2022-01-25 13:08:10 -08:00
|
|
|
|
|
|
|
context "submitting a malformed announcement" do
|
|
|
|
before(:each) { sign_in(user) }
|
|
|
|
|
|
|
|
let :announcement_params do
|
|
|
|
{
|
2022-01-25 13:09:07 -08:00
|
|
|
content: "I like dogs!",
|
2022-01-25 13:08:10 -08:00
|
|
|
starts_at: Time.current,
|
2022-01-25 13:09:07 -08:00
|
|
|
ends_at: 2.days.ago
|
2022-01-25 13:08:10 -08:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
subject do
|
|
|
|
patch :update, params: {
|
2022-01-25 13:09:07 -08:00
|
|
|
id: announcement.id,
|
2022-01-25 13:08:10 -08:00
|
|
|
announcement: announcement_params
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it "stays in the edit view when a malformed announcement is submitted" do
|
|
|
|
subject
|
|
|
|
expect(response).to render_template(:edit)
|
|
|
|
end
|
|
|
|
end
|
2020-04-19 14:40:06 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "#destroy" do
|
|
|
|
let! :announcement do
|
2022-01-25 13:09:07 -08:00
|
|
|
Announcement.create(content: "Dogs are pretty cool, I guess",
|
|
|
|
starts_at: 3.days.from_now,
|
|
|
|
ends_at: 10.days.from_now,
|
|
|
|
user: user)
|
2020-04-19 14:40:06 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
subject { delete :destroy, params: { id: announcement.id } }
|
|
|
|
|
|
|
|
context "user signed in" do
|
|
|
|
before(:each) { sign_in(user) }
|
|
|
|
|
|
|
|
it "deletes the announcement" do
|
|
|
|
expect { subject }.to change { Announcement.count }.by(-1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "redirects to announcement#index" do
|
|
|
|
subject
|
|
|
|
expect(response).to redirect_to(:announcement_index)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|