forked from Gitlink/forgeplus
193 lines
7.0 KiB
Ruby
193 lines
7.0 KiB
Ruby
require 'rails_helper'
|
|
|
|
RSpec.describe 'Sponsorships', type: :request do
|
|
|
|
let(:valid_attributes) {
|
|
{amount: 10, visible: 1, developer_id: 1, sponsor_id: 5}
|
|
}
|
|
|
|
let(:valid_create_api_attributes) {
|
|
{amount: 10, visible: 1, developer_id: 1}
|
|
}
|
|
|
|
let(:invalid_user_attributes) {
|
|
{amount: 10, visible: 1, sponsor_id: 4, developer_id: 1}
|
|
}
|
|
let(:invalid_attributes) {
|
|
{amunt: 10, visible: 1, developid: 1, sponsoid: 5}
|
|
}
|
|
let(:developer_session) { {www_user_id: 5} }
|
|
let(:visitor_session) { {www_user_id: 4} }
|
|
let(:valid_session) { {www_user_id: 6} }
|
|
|
|
describe 'create sponsorship' do
|
|
context 'success' do
|
|
it 'create a new sponsorship' do
|
|
allow_any_instance_of(ActionDispatch::Request).to receive(:session) { valid_session }
|
|
expect {
|
|
post '/api/sponsorships.json', params: {'amount'=>5, 'visible'=>1, single: false, 'developer_id'=>1, 'sponsorship'=>{'amount'=>5, 'visible'=>1, 'developer_id'=>1}}, as: :json
|
|
}.to change(Sponsorship, :count).by(1)
|
|
end
|
|
end
|
|
|
|
context 'single success' do
|
|
it 'create a stopped_sponsorship' do
|
|
allow_any_instance_of(ActionDispatch::Request).to receive(:session) { valid_session }
|
|
expect {
|
|
post '/api/sponsorships.json', params: {'amount'=>5, 'visible'=>1, single: true, 'developer_id'=>1, 'sponsorship'=>{'amount'=>5, 'visible'=>1, 'developer_id'=>1}}, as: :json
|
|
}.to change(StoppedSponsorship, :count).by(1)
|
|
end
|
|
end
|
|
|
|
context 'fail to repeat sponsorship' do
|
|
it 'returns fail' do
|
|
allow_any_instance_of(ActionDispatch::Request).to receive(:session) { valid_session }
|
|
post '/api/sponsorships.json', params: {'amount'=>5, 'visible'=>1, single: false, 'developer_id'=>4, 'sponsorship'=>{'amount'=>5, 'visible'=>1, 'developer_id'=>4}}, as: :json
|
|
expect(JSON.parse(response.body)['message']).to eq('您已经赞助了TA')
|
|
end
|
|
end
|
|
|
|
context 'balance not enough' do
|
|
it 'returns fail' do
|
|
allow_any_instance_of(ActionDispatch::Request).to receive(:session) { valid_session }
|
|
expect {
|
|
post '/api/sponsorships.json', params: {'amount'=>500000, 'visible'=>1, single: false, 'developer_id'=>1, 'sponsorship'=>{'amount'=>500000, 'visible'=>1, 'developer_id'=>1}}, as: :json
|
|
}.to change(Sponsorship, :count).by(0)
|
|
end
|
|
end
|
|
end
|
|
|
|
describe 'index' do
|
|
context 'success' do
|
|
it 'returns n records' do
|
|
get '/api/sponsorships.json'
|
|
expect(JSON.parse(response.body).length).to eq(Sponsorship.count)
|
|
end
|
|
end
|
|
end
|
|
|
|
describe 'stopped' do
|
|
context 'success' do
|
|
it 'returns n records' do
|
|
get '/api/sponsorships/stopped.json'
|
|
expect(JSON.parse(response.body).length).to eq(StoppedSponsorship.count)
|
|
end
|
|
end
|
|
end
|
|
|
|
describe 'sponsored' do
|
|
context 'valid_session' do
|
|
it 'returns 2 records' do
|
|
allow_any_instance_of(ActionDispatch::Request).to receive(:session) { valid_session }
|
|
get '/api/sponsorships/sponsored.json', params: {id: 6}
|
|
expect(JSON.parse(response.body)['sponsorships'].length).to eq(2)
|
|
end
|
|
end
|
|
|
|
context 'visitor session' do
|
|
it 'returns 1 record' do
|
|
allow_any_instance_of(ActionDispatch::Request).to receive(:session) { visitor_session }
|
|
get '/api/sponsorships/sponsored.json', params: {id: 6}
|
|
expect(JSON.parse(response.body)['sponsorships'].length).to eq(1)
|
|
end
|
|
end
|
|
end
|
|
|
|
describe 'sponsoring' do
|
|
context 'valid_session' do
|
|
it 'returns 2 records' do
|
|
allow_any_instance_of(ActionDispatch::Request).to receive(:session) { valid_session }
|
|
get '/api/sponsorships/sponsoring.json', params: {id: 6}
|
|
expect(JSON.parse(response.body)['sponsorships'].length).to eq(2)
|
|
end
|
|
end
|
|
|
|
context 'visitor session' do
|
|
it 'returns 1 record' do
|
|
allow_any_instance_of(ActionDispatch::Request).to receive(:session) { visitor_session }
|
|
get '/api/sponsorships/sponsoring.json', params: {id: 6}
|
|
expect(JSON.parse(response.body)['sponsorships'].length).to eq(1)
|
|
end
|
|
end
|
|
end
|
|
|
|
describe 'stopped_sponsored' do
|
|
context 'valid_session' do
|
|
it 'returns 2 records' do
|
|
allow_any_instance_of(ActionDispatch::Request).to receive(:session) { valid_session }
|
|
get '/api/sponsorships/stopped_sponsored.json', params: {id: 6}
|
|
expect(JSON.parse(response.body)['sponsorships'].length).to eq(2)
|
|
end
|
|
end
|
|
|
|
context 'visitor session' do
|
|
it 'returns 1 record' do
|
|
allow_any_instance_of(ActionDispatch::Request).to receive(:session) { visitor_session }
|
|
get '/api/sponsorships/stopped_sponsored.json', params: {id: 6}
|
|
expect(JSON.parse(response.body)['sponsorships'].length).to eq(1)
|
|
end
|
|
end
|
|
end
|
|
|
|
describe 'stopped_sponsoring' do
|
|
context 'valid_session' do
|
|
it 'returns 2 records' do
|
|
allow_any_instance_of(ActionDispatch::Request).to receive(:session) { valid_session }
|
|
get '/api/sponsorships/stopped_sponsoring.json', params: {id: 6}
|
|
expect(JSON.parse(response.body)['sponsorships'].length).to eq(2)
|
|
end
|
|
end
|
|
|
|
context 'visitor session' do
|
|
it 'returns 1 record' do
|
|
allow_any_instance_of(ActionDispatch::Request).to receive(:session) { visitor_session }
|
|
get '/api/sponsorships/stopped_sponsoring.json', params: {id: 6}
|
|
expect(JSON.parse(response.body)['sponsorships'].length).to eq(1)
|
|
end
|
|
end
|
|
end
|
|
|
|
describe 'update' do
|
|
context 'as sponsor' do
|
|
it 'update success' do
|
|
allow_any_instance_of(ActionDispatch::Request).to receive(:session) { valid_session }
|
|
put '/api/sponsorships/97.json', params: {amount: 50, sponsorship: {amount: 50}}, as: :json
|
|
expect(Sponsorship.find(97).amount).to eq(50)
|
|
expect(response.body).to eq('{"status":1,"message":"修改成功"}')
|
|
end
|
|
end
|
|
|
|
context 'as others' do
|
|
it 'fail' do
|
|
allow_any_instance_of(ActionDispatch::Request).to receive(:session) { developer_session }
|
|
put '/api/sponsorships/97.json', params: {amount: 50, sponsorship: {amount: 50}}, as: :json
|
|
expect(Sponsorship.find(97).amount).to eq(10)
|
|
expect(response.body).to eq('{"status":401,"message":"没有权限"}')
|
|
end
|
|
end
|
|
end
|
|
|
|
describe 'destroy' do
|
|
context 'as sponsor' do
|
|
it 'success' do
|
|
allow_any_instance_of(ActionDispatch::Request).to receive(:session) { valid_session }
|
|
expect{ delete '/api/sponsorships/100.json'}.to change(StoppedSponsorship, :count).by(1)
|
|
end
|
|
end
|
|
|
|
context 'as developer' do
|
|
it 'success' do
|
|
allow_any_instance_of(ActionDispatch::Request).to receive(:session) { developer_session }
|
|
expect{ delete '/api/sponsorships/100.json'}.to change(StoppedSponsorship, :count).by(1)
|
|
end
|
|
end
|
|
|
|
context 'as others' do
|
|
it 'fail' do
|
|
allow_any_instance_of(ActionDispatch::Request).to receive(:session) { visitor_session }
|
|
expect{ delete '/api/sponsorships/100.json'}.to change(StoppedSponsorship, :count).by(0)
|
|
end
|
|
end
|
|
end
|
|
end
|