151 lines
4.4 KiB
Ruby
151 lines
4.4 KiB
Ruby
require 'rails_helper'
|
||
|
||
RSpec.describe UsersController, type: :controller do
|
||
let(:user) { build(:user_with_sequence_number) }
|
||
let(:user_with_fixed_info){ create(:user)}
|
||
let(:valid_attributes){ attributes_for(:user) }
|
||
let(:invalid_attributes){ attributes_for(:user, email: nil, name: 'username2000') }
|
||
|
||
describe 'GET #emailExist' do
|
||
it 'valid email' do
|
||
get :emailExist, email: user.email
|
||
expect(response.body).to eq 'not exist'
|
||
end
|
||
|
||
it 'duplicated email' do
|
||
user = create(:user_with_sequence_number)
|
||
get :emailExist, email: user.email
|
||
expect(response.body).to eq 'exist'
|
||
end
|
||
|
||
it 'should not throw exception with no email param' do
|
||
get :emailExist # 不会失败,则证明没有异常
|
||
get :emailExist, other_param: 'test'
|
||
end
|
||
end
|
||
|
||
describe 'GET #usernameExist' do
|
||
it 'valid username' do
|
||
get :usernameExist, username: user.name
|
||
expect(response.body).to eq 'not exist'
|
||
end
|
||
|
||
it 'duplicated username' do
|
||
user = create(:user_with_sequence_number)
|
||
get :usernameExist, username: user.name
|
||
expect(response.body).to eq 'exist'
|
||
end
|
||
|
||
it 'should not throw exception with no username param' do
|
||
get :usernameExist
|
||
get :usernameExist, other_param: 'test'
|
||
end
|
||
end
|
||
|
||
describe 'POST #create' do
|
||
context 'success with valid attributes' do
|
||
before :each do
|
||
post :create, user: valid_attributes
|
||
end
|
||
|
||
it 'should create user' do
|
||
expect(User.exists?(assigns[:user].id)).to be true
|
||
end
|
||
|
||
it 'should response with 201' do
|
||
expect(response).to have_http_status :created
|
||
end
|
||
|
||
end
|
||
|
||
context 'fail with invalid attributes' do
|
||
# 已经在model测试中充分验证校对条件,所以这里只对使用电子邮箱为空的非法条件
|
||
before :each do
|
||
post :create, user: invalid_attributes
|
||
end
|
||
|
||
it 'does not save the new user' do
|
||
expect(User.exists? name: 'username2000').to be false
|
||
end
|
||
|
||
it 'should return errors' do
|
||
error_message = JSON.parse response.body
|
||
expect(error_message['email'].nil?).to be false
|
||
expect(error_message['email']).not_to be_empty
|
||
end
|
||
end
|
||
|
||
context 'deal with params more or less than required' do
|
||
|
||
let(:data_to_send) { { :name => 'username', :email => '1261138729@qq.com',
|
||
:password => 'secret', :password_confirmation => 'secret',
|
||
:more_field => 'test'} }
|
||
|
||
it 'should throw exception without param[:user]' do
|
||
expect {
|
||
post :create
|
||
}.to raise_error ActionController::ParameterMissing
|
||
end
|
||
|
||
it 'should not throw exception' do
|
||
expect {
|
||
post :create, user: data_to_send, other_param: 'test'
|
||
}.to_not raise_error
|
||
end
|
||
|
||
it 'should not accept other params' do
|
||
post :create, user: data_to_send
|
||
user = assigns(:user)
|
||
expect(user.has_attribute? :more_field).to eq false
|
||
end
|
||
end
|
||
end
|
||
|
||
describe 'PATCH #update' do
|
||
|
||
context 'with valid attributes' do
|
||
|
||
before :each do
|
||
# 假设已经登录了
|
||
allow(controller).to receive(:authenticate){ true }
|
||
allow(controller).to receive(:current_user).and_return(User.find user_with_fixed_info.id)
|
||
patch :update, user: valid_attributes, format: 'json'
|
||
end
|
||
|
||
it 'should located current user' do
|
||
expect(assigns[:user]).to eq user_with_fixed_info
|
||
end
|
||
|
||
it 'should render show' do
|
||
expect(response).to render_template 'show'
|
||
end
|
||
|
||
end
|
||
|
||
context 'with invalid attributes' do
|
||
before :each do
|
||
# 假设已经登录了
|
||
allow(controller).to receive(:authenticate){ true }
|
||
allow(controller).to receive(:current_user).and_return(User.find user_with_fixed_info.id)
|
||
patch :update, user: invalid_attributes, format: 'json'
|
||
end
|
||
|
||
it 'does not change current user' do
|
||
expect(assigns[:user]).to eq user_with_fixed_info
|
||
end
|
||
|
||
it 'should return error messages and error status' do
|
||
error_message = JSON.parse response.body
|
||
expect(error_message['email'].nil?).to be false
|
||
expect(error_message['email']).not_to be_empty
|
||
end
|
||
|
||
end
|
||
|
||
it 'should login first' do
|
||
patch :update, user:valid_attributes, format: 'json'
|
||
expect(response).to have_http_status :unauthorized
|
||
end
|
||
end
|
||
end
|