From 3de8de9a41996dce746a927668be3f53c5315641 Mon Sep 17 00:00:00 2001 From: elviabth Date: Thu, 18 Apr 2024 09:42:33 +0200 Subject: [PATCH] add checks to controller spec --- .../controller/admin/codes_controller_spec.rb | 31 +++++++++++++++---- 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/spec/controller/admin/codes_controller_spec.rb b/spec/controller/admin/codes_controller_spec.rb index 8c9b31a..4a07f95 100644 --- a/spec/controller/admin/codes_controller_spec.rb +++ b/spec/controller/admin/codes_controller_spec.rb @@ -9,17 +9,36 @@ module Admin routes { Decidim::AnonymousCodes::AdminEngine.routes } let(:current_user) { create(:user, :confirmed, :admin, organization: current_organization) } let(:current_organization) { create(:organization) } - let(:component) { survey.component } - let!(:survey) { create(:survey) } - let!(:existing_group) { create(:anonymous_codes_group, title: { en: "Existing group" }, organization: organization, resource: survey) } - let!(:existing_empty_group) { create(:anonymous_codes_group, title: { en: "Existing empty group" }, organization: organization, resource: survey) } - let!(:anonymous_codes_token1) { create(:anonymous_codes_token, group: existing_group) } - let!(:anonymous_codes_token2) { create(:anonymous_codes_token, :used, group: existing_group) } + let!(:group) { create(:anonymous_codes_group, expires_at: 1.day.from_now, active: true, max_reuses: 10, organization: current_organization) } before do request.env["decidim.current_organization"] = current_organization sign_in current_user, scope: :user end + + describe "GET #index" do + let!(:token1) { Token.create(group: group, created_at: 2.days.ago) } + let!(:token2) { Token.create(group: group, created_at: 1.day.ago) } + + it "enforces permission to view anonymous code tokens" do + expect(controller).to receive(:enforce_permission_to).with(:view, :anonymous_code_token) + get :index, params: { code_group_id: code_group.id } + end + + it "assigns @tokens with paginated tokens ordered by creation date" do + token1 + token2 + + get :index, params: { code_group_id: code_group.id } + + expect(assigns(:tokens)).to eq([token2, token1]) + end + + it "renders the index template" do + get :index, params: { code_group_id: code_group.id } + expect(response).to render_template(:index) + end + end end end end