DEV: Reduce duplication

This commit is contained in:
Daniel Waterworth 2019-11-01 13:00:40 +00:00
parent f9a05e4ae7
commit 2b8a013e32
1 changed files with 15 additions and 18 deletions

View File

@ -3,6 +3,12 @@
require 'rails_helper' require 'rails_helper'
describe TagsController do describe TagsController do
define_method(:get_json_body) do
::JSON.parse(response.body)
end
let(:json) { get_json_body }
fab!(:user) { Fabricate(:user) } fab!(:user) { Fabricate(:user) }
fab!(:admin) { Fabricate(:admin) } fab!(:admin) { Fabricate(:admin) }
fab!(:regular_user) { Fabricate(:trust_level_4) } fab!(:regular_user) { Fabricate(:trust_level_4) }
@ -28,7 +34,7 @@ describe TagsController do
expect(response.status).to eq(200) expect(response.status).to eq(200)
tags = JSON.parse(response.body)["tags"] tags = json["tags"]
expect(tags.length).to eq(1) expect(tags.length).to eq(1)
expect(tags[0]['text']).to eq("topic-test") expect(tags[0]['text']).to eq("topic-test")
end end
@ -53,7 +59,7 @@ describe TagsController do
expect(response.status).to eq(200) expect(response.status).to eq(200)
tags = JSON.parse(response.body)["tags"] tags = json["tags"]
expect(tags.length).to eq(2) expect(tags.length).to eq(2)
end end
@ -94,7 +100,7 @@ describe TagsController do
expect(response.status).to eq(200) expect(response.status).to eq(200)
response_tag = JSON.parse(response.body)["valid"].first response_tag = json["valid"].first
expect(response_tag["value"]).to eq(tag.name) expect(response_tag["value"]).to eq(tag.name)
end end
end end
@ -160,7 +166,7 @@ describe TagsController do
expect(response.status).to eq(200) expect(response.status).to eq(200)
tag = JSON.parse(response.body)['tags'] tag = json['tags']
expect(tag[0]["id"]).to eq('test') expect(tag[0]["id"]).to eq('test')
end end
end end
@ -175,7 +181,7 @@ describe TagsController do
expect(response.status).to eq(200) expect(response.status).to eq(200)
tag = JSON.parse(response.body)['tags'] tag = json['tags']
expect(tag[0]["id"]).to eq('test') expect(tag[0]["id"]).to eq('test')
end end
@ -184,7 +190,7 @@ describe TagsController do
expect(response.status).to eq(200) expect(response.status).to eq(200)
tag = JSON.parse(response.body)['tags'] tag = json['tags']
expect(tag[0]["id"]).to eq('test') expect(tag[0]["id"]).to eq('test')
end end
end end
@ -209,7 +215,7 @@ describe TagsController do
context 'tagging enabled' do context 'tagging enabled' do
def parse_topic_ids def parse_topic_ids
JSON.parse(response.body)["topic_list"]["topics"] get_json_body["topic_list"]["topics"]
.map { |topic| topic["id"] } .map { |topic| topic["id"] }
end end
@ -353,7 +359,6 @@ describe TagsController do
tag_names.each { |name| Fabricate(:tag, name: name) } tag_names.each { |name| Fabricate(:tag, name: name) }
get "/tags/filter/search.json", params: { q: 'stu' } get "/tags/filter/search.json", params: { q: 'stu' }
expect(response.status).to eq(200) expect(response.status).to eq(200)
json = ::JSON.parse(response.body)
expect(json["results"].map { |j| j["id"] }.sort).to eq(['stuff', 'stumped']) expect(json["results"].map { |j| j["id"] }.sort).to eq(['stuff', 'stumped'])
end end
@ -364,7 +369,6 @@ describe TagsController do
get '/tags/filter/search.json', params: { q: 'tag', limit: 2 } get '/tags/filter/search.json', params: { q: 'tag', limit: 2 }
expect(response.status).to eq(200) expect(response.status).to eq(200)
json = ::JSON.parse(response.body)
expect(json['results'].map { |j| j['id'] }).to eq(['tag', 'tag2']) expect(json['results'].map { |j| j['id'] }).to eq(['tag', 'tag2'])
end end
@ -376,7 +380,6 @@ describe TagsController do
nope = Fabricate(:tag, name: 'nope') nope = Fabricate(:tag, name: 'nope')
get "/tags/filter/search.json", params: { q: nope.name, categoryId: category.id } get "/tags/filter/search.json", params: { q: nope.name, categoryId: category.id }
expect(response.status).to eq(200) expect(response.status).to eq(200)
json = ::JSON.parse(response.body)
expect(json["results"].map { |j| j["id"] }.sort).to eq([]) expect(json["results"].map { |j| j["id"] }.sort).to eq([])
expect(json["forbidden"]).to be_present expect(json["forbidden"]).to be_present
expect(json["forbidden_message"]).to eq(I18n.t("tags.forbidden.in_this_category", tag_name: nope.name)) expect(json["forbidden_message"]).to eq(I18n.t("tags.forbidden.in_this_category", tag_name: nope.name))
@ -385,7 +388,6 @@ describe TagsController do
it "can say if given tag is restricted to different category" do it "can say if given tag is restricted to different category" do
category category
get "/tags/filter/search.json", params: { q: yup.name, categoryId: Fabricate(:category).id } get "/tags/filter/search.json", params: { q: yup.name, categoryId: Fabricate(:category).id }
json = ::JSON.parse(response.body)
expect(json["results"].map { |j| j["id"] }.sort).to eq([]) expect(json["results"].map { |j| j["id"] }.sort).to eq([])
expect(json["forbidden"]).to be_present expect(json["forbidden"]).to be_present
expect(json["forbidden_message"]).to eq(I18n.t( expect(json["forbidden_message"]).to eq(I18n.t(
@ -400,7 +402,6 @@ describe TagsController do
nope = Fabricate(:tag, name: 'nope') nope = Fabricate(:tag, name: 'nope')
get "/tags/filter/search.json", params: { categoryId: category.id } get "/tags/filter/search.json", params: { categoryId: category.id }
expect(response.status).to eq(200) expect(response.status).to eq(200)
json = ::JSON.parse(response.body)
expect(json["results"].map { |j| j["id"] }.sort).to eq([yup.name]) expect(json["results"].map { |j| j["id"] }.sort).to eq([yup.name])
end end
end end
@ -409,7 +410,6 @@ describe TagsController do
yup, nope = Fabricate(:tag, name: 'yup'), Fabricate(:tag, name: 'nope') yup, nope = Fabricate(:tag, name: 'yup'), Fabricate(:tag, name: 'nope')
get "/tags/filter/search.json", params: { q: 'N/ope' } get "/tags/filter/search.json", params: { q: 'N/ope' }
expect(response.status).to eq(200) expect(response.status).to eq(200)
json = ::JSON.parse(response.body)
expect(json["results"].map { |j| j["id"] }.sort).to eq(["nope"]) expect(json["results"].map { |j| j["id"] }.sort).to eq(["nope"])
end end
@ -418,7 +418,6 @@ describe TagsController do
Fabricate(:topic, category: c, tags: [Fabricate(:tag, name: "cooltag")]) Fabricate(:topic, category: c, tags: [Fabricate(:tag, name: "cooltag")])
get "/tags/filter/search.json", params: { q: "cool" } get "/tags/filter/search.json", params: { q: "cool" }
expect(response.status).to eq(200) expect(response.status).to eq(200)
json = ::JSON.parse(response.body)
expect(json["results"].map { |j| j["id"] }).to eq(['cooltag']) expect(json["results"].map { |j| j["id"] }).to eq(['cooltag'])
end end
@ -428,12 +427,12 @@ describe TagsController do
get "/tags/filter/search.json", params: { q: '房' } get "/tags/filter/search.json", params: { q: '房' }
expect(response.status).to eq(200) expect(response.status).to eq(200)
json = ::JSON.parse(response.body) json = get_json_body
expect(json["results"].map { |j| j["id"] }).to eq(['房地产']) expect(json["results"].map { |j| j["id"] }).to eq(['房地产'])
get "/tags/filter/search.json", params: { q: 'тема' } get "/tags/filter/search.json", params: { q: 'тема' }
expect(response.status).to eq(200) expect(response.status).to eq(200)
json = ::JSON.parse(response.body) json = get_json_body
expect(json["results"].map { |j| j["id"] }).to eq(['тема-в-разработке']) expect(json["results"].map { |j| j["id"] }).to eq(['тема-в-разработке'])
end end
end end
@ -458,7 +457,6 @@ describe TagsController do
it 'returns a tag not found message' do it 'returns a tag not found message' do
delete "/tags/doesntexists.json" delete "/tags/doesntexists.json"
expect(response).not_to be_successful expect(response).not_to be_successful
json = ::JSON.parse(response.body)
expect(json['error_type']).to eq('not_found') expect(json['error_type']).to eq('not_found')
end end
end end
@ -491,7 +489,6 @@ describe TagsController do
it 'returns the correct unused tags' do it 'returns the correct unused tags' do
get "/tags/unused.json" get "/tags/unused.json"
expect(response.status).to eq(200) expect(response.status).to eq(200)
json = ::JSON.parse(response.body)
expect(json["tags"]).to contain_exactly("unused1", "unused2") expect(json["tags"]).to contain_exactly("unused1", "unused2")
end end