Skip to content

Commit

Permalink
create specs for commands and controller methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ElviaBth committed May 2, 2024
1 parent b497813 commit 3f42e1d
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 2 deletions.
2 changes: 1 addition & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ en:
destroy:
success: Access code token successfully destroyed
form:
code: Token code
code: Token
index:
available: Available?
back: Back to groups
Expand Down
34 changes: 34 additions & 0 deletions spec/commands/admin/create_token_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

require "spec_helper"

module Decidim
module AnonymousCodes
module Admin
describe CreateTokens, type: :command do
let(:form) { double("FormObject", invalid?: invalid, token: "1SFDRG5SA") }
let(:code_group) { create(:anonymous_codes_group) }
let(:command) { described_class.new(form, code_group) }
let(:invalid) { false }

context "when the form is not valid" do
let(:invalid) { true }

it "is not valid" do
expect { command.call }.to broadcast(:invalid)
end
end

context "when the form is valid" do
it "broadcasts ok" do
expect { command.call }.to broadcast(:ok)
end

it "creates a new token" do
expect { command.call }.to change(Token, :count).by(1)
end
end
end
end
end
end
53 changes: 52 additions & 1 deletion spec/controller/admin/codes_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,22 @@ module Admin
end
end

# TODO: Get #new & #create
describe "GET #new" do
it "enforces permission to create anonymous code tokens" do
expect(controller).to receive(:enforce_permission_to).with(:create, :anonymous_code_token)
get :new, params: { code_group_id: code_group.id }
end

it "assigns a new instance of TokensForm to @form" do
get :new, params: { code_group_id: code_group.id }
expect(assigns(:form)).to be_an_instance_of(TokensForm)
end

it "renders the new template" do
get :new, params: { code_group_id: code_group.id }
expect(response).to render_template(:new)
end
end

describe "GET #bulk" do
it "enforces permission to create anonymous code tokens" do
Expand Down Expand Up @@ -96,6 +111,42 @@ module Admin
end
end

describe "POST #create" do
context "with valid parameters" do
let(:valid_params) do
{
code_group_id: code_group.id,
token: "5RGTBSSSSSGJ678"
}
end

it "creates a new token and redirects to code group codes path" do
expect do
post :create, params: valid_params
end.to change(Token, :count).by(1)

expect(response).to redirect_to(code_group_codes_path(code_group))
expect(flash[:notice]).to be_present
end
end

context "with invalid parameters" do
let(:invalid_params) do
{
code_group_id: code_group.id,
token: "5RGT@+*asfhveiw6"
}
end

it "renders the new template with an alert message" do
post :create, params: invalid_params

expect(response).to render_template("new")
expect(flash[:alert]).to be_present
end
end
end

describe "DELETE #destroy" do
let(:token) { create(:anonymous_codes_token, group: code_group, created_at: 2.days.ago) }
let!(:group) { create(:anonymous_codes_group, expires_at: 1.day.from_now, active: true, max_reuses: 10, organization: current_organization) }
Expand Down

0 comments on commit 3f42e1d

Please sign in to comment.