DEV: Restore order assertion in category serializer tests. (#16344)

Our group fabrication creates groups with name "my_group_#{n}" where n
is the sequence number of the group being created. However, this can
cause the test to be flaky if and when a group with name `my_group_10`
is created as it will be ordered before
`my_group_9`. This commits makes the group names determinstic to
eliminate any flakiness.

This reverts commit 558bc6b746928a1fcdb2773b0447ea4a57aa7419.
This commit is contained in:
Alan Guo Xiang Tan
2022-04-01 08:58:06 +08:00
committed by GitHub
parent 4b93dba82f
commit e7c3d01aaa
3 changed files with 15 additions and 13 deletions

View File

@ -142,7 +142,7 @@ class Group < ActiveRecord::Base
scope :with_smtp_configured, -> { where(smtp_enabled: true) } scope :with_smtp_configured, -> { where(smtp_enabled: true) }
scope :visible_groups, Proc.new { |user, order, opts| scope :visible_groups, Proc.new { |user, order, opts|
groups = self.order(order || "name ASC") groups = self.order(order || "groups.name ASC")
if !opts || !opts[:include_everyone] if !opts || !opts[:include_everyone]
groups = groups.where("groups.id > 0") groups = groups.where("groups.id > 0")

View File

@ -37,8 +37,8 @@ class CategorySerializer < SiteCategorySerializer
.category_groups .category_groups
.joins(:group) .joins(:group)
.includes(:group) .includes(:group)
.merge(Group.visible_groups(scope&.user)) .merge(Group.visible_groups(scope&.user, "groups.name ASC"))
.order("groups.name").map do |cg| .map do |cg|
{ {
permission_type: cg.permission_type, permission_type: cg.permission_type,
group_name: cg.group.name group_name: cg.group.name

View File

@ -54,15 +54,17 @@ describe CategorySerializer do
end end
context "category with group permissions configured" do context "category with group permissions configured" do
fab!(:private_group) { Fabricate(:group, visibility_level: Group.visibility_levels[:staff]) } fab!(:private_group) { Fabricate(:group, visibility_level: Group.visibility_levels[:staff], name: 'bbb') }
fab!(:user_group) do fab!(:user_group) do
Fabricate(:group, visibility_level: Group.visibility_levels[:members]).tap do |g| Fabricate(:group, visibility_level: Group.visibility_levels[:members], name: 'ccc').tap do |g|
g.add(user) g.add(user)
end end
end end
before do before do
group.update!(name: 'aaa')
category.set_permissions( category.set_permissions(
:everyone => :readonly, :everyone => :readonly,
group.name => :readonly, group.name => :readonly,
@ -76,28 +78,28 @@ describe CategorySerializer do
it "returns the right category group permissions for an anon user" do it "returns the right category group permissions for an anon user" do
json = described_class.new(category, scope: Guardian.new, root: false).as_json json = described_class.new(category, scope: Guardian.new, root: false).as_json
expect(json[:group_permissions]).to contain_exactly( expect(json[:group_permissions]).to eq([
{ permission_type: CategoryGroup.permission_types[:readonly], group_name: group.name }, { permission_type: CategoryGroup.permission_types[:readonly], group_name: group.name },
) ])
end end
it "returns the right category group permissions for a regular user" do it "returns the right category group permissions for a regular user ordered by ascending group name" do
json = described_class.new(category, scope: Guardian.new(user), root: false).as_json json = described_class.new(category, scope: Guardian.new(user), root: false).as_json
expect(json[:group_permissions]).to contain_exactly( expect(json[:group_permissions]).to eq([
{ permission_type: CategoryGroup.permission_types[:readonly], group_name: group.name }, { permission_type: CategoryGroup.permission_types[:readonly], group_name: group.name },
{ permission_type: CategoryGroup.permission_types[:full], group_name: user_group.name }, { permission_type: CategoryGroup.permission_types[:full], group_name: user_group.name },
) ])
end end
it "returns the right category group permission for a staff user" do it "returns the right category group permission for a staff user ordered by ascending group name" do
json = described_class.new(category, scope: Guardian.new(admin), root: false).as_json json = described_class.new(category, scope: Guardian.new(admin), root: false).as_json
expect(json[:group_permissions]).to contain_exactly( expect(json[:group_permissions]).to eq([
{ permission_type: CategoryGroup.permission_types[:readonly], group_name: group.name }, { permission_type: CategoryGroup.permission_types[:readonly], group_name: group.name },
{ permission_type: CategoryGroup.permission_types[:full], group_name: private_group.name }, { permission_type: CategoryGroup.permission_types[:full], group_name: private_group.name },
{ permission_type: CategoryGroup.permission_types[:full], group_name: user_group.name } { permission_type: CategoryGroup.permission_types[:full], group_name: user_group.name }
) ])
end end
end end
end end