DEV: avoid double sign-in which can lead to flaky tests

DEV: avoid double sign-in which can lead to flaky tests

We should not be signing in twice in tests, it is both wasteful and risky

diff --git a/spec/requests/category_hashtags_controller_spec.rb b/spec/requests/category_hashtags_controller_spec.rb
index d31cea6..f055998 100644
--- a/spec/requests/category_hashtags_controller_spec.rb
+++ b/spec/requests/category_hashtags_controller_spec.rb
@@ -5,44 +5,49 @@ require 'rails_helper'
 describe CategoryHashtagsController do
   describe "check" do
     describe "logged in" do
-      before do
-        sign_in(Fabricate(:user))
-      end
 
-      it 'only returns the categories that are valid' do
-        category = Fabricate(:category)
+      describe "regular user" do
+        before do
+          sign_in(Fabricate(:user))
+        end
 
-        get "/category_hashtags/check.json", params: { category_slugs: [category.slug, 'none'] }
+        it 'only returns the categories that are valid' do
+          category = Fabricate(:category)
 
-        expect(response.status).to eq(200)
-        expect(JSON.parse(response.body)).to eq(
-          "valid" => [{ "slug" => category.hashtag_slug, "url" => category.url_with_id }]
-        )
-      end
+          get "/category_hashtags/check.json", params: { category_slugs: [category.slug, 'none'] }
 
-      it 'does not return restricted categories for a normal user' do
-        group = Fabricate(:group)
-        private_category = Fabricate(:private_category, group: group)
+          expect(response.status).to eq(200)
+          expect(JSON.parse(response.body)).to eq(
+            "valid" => [{ "slug" => category.hashtag_slug, "url" => category.url_with_id }]
+          )
+        end
 
-        get "/category_hashtags/check.json", params: { category_slugs: [private_category.slug] }
+        it 'does not return restricted categories for a normal user' do
+          group = Fabricate(:group)
+          private_category = Fabricate(:private_category, group: group)
 
-        expect(response.status).to eq(200)
-        expect(JSON.parse(response.body)).to eq("valid" => [])
-      end
+          get "/category_hashtags/check.json", params: { category_slugs: [private_category.slug] }
 
-      it 'returns restricted categories for an admin' do
-        admin = sign_in(Fabricate(:admin))
-        group = Fabricate(:group)
-        group.add(admin)
-        private_category = Fabricate(:private_category, group: group)
-
-        get "/category_hashtags/check.json",
-          params: { category_slugs: [private_category.slug] }
+          expect(response.status).to eq(200)
+          expect(JSON.parse(response.body)).to eq("valid" => [])
+        end
+      end
 
-        expect(response.status).to eq(200)
-        expect(JSON.parse(response.body)).to eq(
-          "valid" => [{ "slug" => private_category.hashtag_slug, "url" => private_category.url_with_id }]
-        )
+      describe "admin user" do
+        it 'returns restricted categories for an admin' do
+          admin = sign_in(Fabricate(:admin))
+          group = Fabricate(:group)
+          group.add(admin)
+          private_category = Fabricate(:private_category, group: group)
+
+          get "/category_hashtags/check.json",
+            params: { category_slugs: [private_category.slug] }
+
+          expect(response.status).to eq(200)
+          expect(JSON.parse(response.body)).to eq(
+            "valid" => [{ "slug" => private_category.hashtag_slug, "url" => private_category.url_with_id }]
+          )
+        end
       end
     end

GitHub sha: f4157122

1 Like