Refactor tests to use the json extension instead of headers.

This commit is contained in:
Guo Xiang Tan 2018-05-21 09:49:46 +08:00
parent 1841dd48dc
commit 2ceb107074
2 changed files with 19 additions and 20 deletions

View File

@ -213,9 +213,9 @@ Discourse::Application.routes.draw do
# They have periods in their URLs often: # They have periods in their URLs often:
get 'site_texts' => 'site_texts#index' get 'site_texts' => 'site_texts#index'
get 'site_texts/(:id)' => 'site_texts#show', constraints: { id: /[\w.\-\+]+/i } get 'site_texts/:id' => 'site_texts#show', constraints: { id: /[\w.\-\+]+/i }
put 'site_texts/(:id)' => 'site_texts#update', constraints: { id: /[\w.\-\+]+/i } put 'site_texts/:id.json' => 'site_texts#update', constraints: { id: /[\w.\-\+]+/i }
delete 'site_texts/(:id)' => 'site_texts#revert', constraints: { id: /[\w.\-\+]+/i } delete 'site_texts/:id' => 'site_texts#revert', constraints: { id: /[\w.\-\+]+/i }
get 'email_templates' => 'email_templates#index' get 'email_templates' => 'email_templates#index'
get 'email_templates/(:id)' => 'email_templates#show', constraints: { id: /[0-9a-z_.]+/ } get 'email_templates/(:id)' => 'email_templates#show', constraints: { id: /[0-9a-z_.]+/ }

View File

@ -3,7 +3,6 @@ require 'rails_helper'
RSpec.describe Admin::SiteTextsController do RSpec.describe Admin::SiteTextsController do
let(:admin) { Fabricate(:admin) } let(:admin) { Fabricate(:admin) }
let(:user) { Fabricate(:user) } let(:user) { Fabricate(:user) }
let(:headers) { { ACCEPT: 'application/json' } }
after do after do
TranslationOverride.delete_all TranslationOverride.delete_all
@ -12,17 +11,20 @@ RSpec.describe Admin::SiteTextsController do
context "#update" do context "#update" do
it "raises an error if you aren't logged in" do it "raises an error if you aren't logged in" do
put '/admin/customize/site_texts/some_key', params: { put '/admin/customize/site_texts/some_key.json', params: {
site_text: { value: 'foo' } site_text: { value: 'foo' }
}, headers: headers }
expect(response.status).to eq(404) expect(response.status).to eq(404)
end end
it "raises an error if you aren't an admin" do it "raises an error if you aren't an admin" do
sign_in(user) sign_in(user)
put '/admin/customize/site_texts/some_key', params: { put '/admin/customize/site_texts/some_key', params: {
site_text: { value: 'foo' } site_text: { value: 'foo' }
}, headers: headers }
expect(response.status).to eq(404) expect(response.status).to eq(404)
end end
@ -32,39 +34,36 @@ RSpec.describe Admin::SiteTextsController do
end end
it "returns 'not found' when an unknown key is used" do it "returns 'not found' when an unknown key is used" do
put '/admin/customize/site_texts/some_key', params: { put '/admin/customize/site_texts/some_key.json', params: {
site_text: { value: 'foo' } site_text: { value: 'foo' }
}, headers: headers }
expect(response).not_to be_success expect(response.status).to eq(404)
json = ::JSON.parse(response.body) json = JSON.parse(response.body)
expect(json['error_type']).to eq('not_found') expect(json['error_type']).to eq('not_found')
end end
it "works as expectd with correct keys" do it "works as expectd with correct keys" do
put '/admin/customize/site_texts/title', params: { put '/admin/customize/site_texts/title.json', params: {
site_text: { value: 'foo' } site_text: { value: 'foo' }
}, headers: headers }
expect(response).to be_success expect(response.status).to eq(200)
json = ::JSON.parse(response.body) json = ::JSON.parse(response.body)
expect(json).to be_present
site_text = json['site_text'] site_text = json['site_text']
expect(site_text).to be_present
expect(site_text['id']).to eq('title') expect(site_text['id']).to eq('title')
expect(site_text['value']).to eq('foo') expect(site_text['value']).to eq('foo')
end end
it "does not update restricted keys" do it "does not update restricted keys" do
put '/admin/customize/site_texts/user_notifications.confirm_old_email.title', params: { put '/admin/customize/site_texts/user_notifications.confirm_old_email.title.json', params: {
site_text: { value: 'foo' } site_text: { value: 'foo' }
}, headers: headers }
expect(response).not_to be_success expect(response.status).to eq(404)
json = ::JSON.parse(response.body) json = ::JSON.parse(response.body)
expect(json['error_type']).to eq('not_found') expect(json['error_type']).to eq('not_found')