|
113 | 113 | end |
114 | 114 | end |
115 | 115 | end |
| 116 | + |
| 117 | + describe "#can_translate?" do |
| 118 | + fab!(:group) |
| 119 | + fab!(:user) { Fabricate(:user, locale: "en", groups: [group]) } |
| 120 | + fab!(:post) |
| 121 | + |
| 122 | + let(:guardian) { Guardian.new(user) } |
| 123 | + |
| 124 | + it "returns false when translator disabled" do |
| 125 | + SiteSetting.translator_enabled = false |
| 126 | + |
| 127 | + expect(guardian.can_translate?(post)).to eq(false) |
| 128 | + end |
| 129 | + |
| 130 | + describe "when translator enabled" do |
| 131 | + before { SiteSetting.translator_enabled = true } |
| 132 | + |
| 133 | + describe "anon user" do |
| 134 | + before { SiteSetting.restrict_translation_by_group = "#{Group::AUTO_GROUPS[:everyone]}" } |
| 135 | + |
| 136 | + it "cannot translate" do |
| 137 | + expect(Guardian.new.can_translate?(post)).to eq(false) |
| 138 | + end |
| 139 | + end |
| 140 | + |
| 141 | + describe "logged in user" do |
| 142 | + it "cannot translate when user is not in restrict_translation_by_group" do |
| 143 | + SiteSetting.restrict_translation_by_group = "#{group.id + 1}" |
| 144 | + |
| 145 | + expect(guardian.can_translate?(post)).to eq(false) |
| 146 | + end |
| 147 | + |
| 148 | + describe "user is in restrict_translation_by_group" do |
| 149 | + before { SiteSetting.restrict_translation_by_group = "#{group.id}" } |
| 150 | + |
| 151 | + describe "locale is :xx" do |
| 152 | + before { I18n.stubs(:locale).returns(:pt) } |
| 153 | + |
| 154 | + it "cannot translate when post does not have DETECTED_LANG_CUSTOM_FIELD" do |
| 155 | + expect(guardian.can_translate?(post)).to eq(false) |
| 156 | + end |
| 157 | + |
| 158 | + it "cannot translate when post has DETECTED_LANG_CUSTOM_FIELD matches locale" do |
| 159 | + post.custom_fields[DiscourseTranslator::DETECTED_LANG_CUSTOM_FIELD] = "pt" |
| 160 | + post.save |
| 161 | + |
| 162 | + expect(guardian.can_translate?(post)).to eq(false) |
| 163 | + end |
| 164 | + |
| 165 | + it "can translate when post has DETECTED_LANG_CUSTOM_FIELD does not match locale" do |
| 166 | + post.custom_fields[DiscourseTranslator::DETECTED_LANG_CUSTOM_FIELD] = "jp" |
| 167 | + post.save |
| 168 | + |
| 169 | + expect(guardian.can_translate?(post)).to eq(true) |
| 170 | + end |
| 171 | + end |
| 172 | + end |
| 173 | + end |
| 174 | + end |
| 175 | + end |
116 | 176 | end |
0 commit comments