2012-09-20 14:44:44 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-04-21 20:32:02 +00:00
|
|
|
describe API::Session do
|
2012-11-06 03:31:55 +00:00
|
|
|
let(:user) { create(:user) }
|
2012-09-20 14:44:44 +00:00
|
|
|
|
|
|
|
describe "POST /session" do
|
|
|
|
context "when valid password" do
|
2016-08-01 15:00:44 +00:00
|
|
|
it "returns private token" do
|
2013-11-25 20:33:04 +00:00
|
|
|
post api("/session"), email: user.email, password: '12345678'
|
2016-06-27 18:10:42 +00:00
|
|
|
expect(response).to have_http_status(201)
|
2012-09-20 14:44:44 +00:00
|
|
|
|
2015-02-12 18:17:35 +00:00
|
|
|
expect(json_response['email']).to eq(user.email)
|
|
|
|
expect(json_response['private_token']).to eq(user.private_token)
|
2017-04-09 02:20:57 +00:00
|
|
|
expect(json_response['is_admin']).to eq(user.admin?)
|
2015-02-12 18:17:35 +00:00
|
|
|
expect(json_response['can_create_project']).to eq(user.can_create_project?)
|
|
|
|
expect(json_response['can_create_group']).to eq(user.can_create_group?)
|
2012-09-20 14:44:44 +00:00
|
|
|
end
|
2016-08-12 21:16:12 +00:00
|
|
|
|
|
|
|
context 'with 2FA enabled' do
|
|
|
|
it 'rejects sign in attempts' do
|
|
|
|
user = create(:user, :two_factor)
|
|
|
|
|
|
|
|
post api('/session'), email: user.email, password: user.password
|
|
|
|
|
|
|
|
expect(response).to have_http_status(401)
|
2016-08-17 22:39:20 +00:00
|
|
|
expect(response.body).to include('You have 2FA enabled.')
|
2016-08-12 21:16:12 +00:00
|
|
|
end
|
|
|
|
end
|
2012-09-20 14:44:44 +00:00
|
|
|
end
|
|
|
|
|
2014-10-22 15:29:26 +00:00
|
|
|
context 'when email has case-typo and password is valid' do
|
2016-08-01 15:00:44 +00:00
|
|
|
it 'returns private token' do
|
2014-10-22 15:29:26 +00:00
|
|
|
post api('/session'), email: user.email.upcase, password: '12345678'
|
|
|
|
expect(response.status).to eq 201
|
|
|
|
|
|
|
|
expect(json_response['email']).to eq user.email
|
|
|
|
expect(json_response['private_token']).to eq user.private_token
|
2017-04-09 02:20:57 +00:00
|
|
|
expect(json_response['is_admin']).to eq user.admin?
|
2014-10-22 15:29:26 +00:00
|
|
|
expect(json_response['can_create_project']).to eq user.can_create_project?
|
|
|
|
expect(json_response['can_create_group']).to eq user.can_create_group?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when login has case-typo and password is valid' do
|
2016-08-01 15:00:44 +00:00
|
|
|
it 'returns private token' do
|
2014-10-22 15:29:26 +00:00
|
|
|
post api('/session'), login: user.username.upcase, password: '12345678'
|
|
|
|
expect(response.status).to eq 201
|
|
|
|
|
|
|
|
expect(json_response['email']).to eq user.email
|
|
|
|
expect(json_response['private_token']).to eq user.private_token
|
2017-04-09 02:20:57 +00:00
|
|
|
expect(json_response['is_admin']).to eq user.admin?
|
2014-10-22 15:29:26 +00:00
|
|
|
expect(json_response['can_create_project']).to eq user.can_create_project?
|
|
|
|
expect(json_response['can_create_group']).to eq user.can_create_group?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-09-20 14:44:44 +00:00
|
|
|
context "when invalid password" do
|
2016-08-01 15:00:44 +00:00
|
|
|
it "returns authentication error" do
|
2012-09-20 14:44:44 +00:00
|
|
|
post api("/session"), email: user.email, password: '123'
|
2016-06-27 18:10:42 +00:00
|
|
|
expect(response).to have_http_status(401)
|
2012-09-20 14:44:44 +00:00
|
|
|
|
2015-02-12 18:17:35 +00:00
|
|
|
expect(json_response['email']).to be_nil
|
|
|
|
expect(json_response['private_token']).to be_nil
|
2012-09-20 14:44:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when empty password" do
|
2016-11-09 16:36:35 +00:00
|
|
|
it "returns authentication error with email" do
|
2012-09-20 14:44:44 +00:00
|
|
|
post api("/session"), email: user.email
|
|
|
|
|
2016-11-09 16:36:35 +00:00
|
|
|
expect(response).to have_http_status(400)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns authentication error with username" do
|
|
|
|
post api("/session"), email: user.username
|
|
|
|
|
|
|
|
expect(response).to have_http_status(400)
|
2012-09-20 14:44:44 +00:00
|
|
|
end
|
|
|
|
end
|
2013-02-27 11:58:06 +00:00
|
|
|
|
|
|
|
context "when empty name" do
|
2016-08-01 15:00:44 +00:00
|
|
|
it "returns authentication error" do
|
2013-02-27 11:58:06 +00:00
|
|
|
post api("/session"), password: user.password
|
|
|
|
|
2016-11-09 16:36:35 +00:00
|
|
|
expect(response).to have_http_status(400)
|
2013-02-27 11:58:06 +00:00
|
|
|
end
|
|
|
|
end
|
2017-01-18 10:23:25 +00:00
|
|
|
|
|
|
|
context "when user is blocked" do
|
|
|
|
it "returns authentication error" do
|
|
|
|
user.block
|
|
|
|
post api("/session"), email: user.username, password: user.password
|
|
|
|
|
|
|
|
expect(response).to have_http_status(401)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when user is ldap_blocked" do
|
|
|
|
it "returns authentication error" do
|
|
|
|
user.ldap_block
|
|
|
|
post api("/session"), email: user.username, password: user.password
|
|
|
|
|
|
|
|
expect(response).to have_http_status(401)
|
|
|
|
end
|
|
|
|
end
|
2012-09-20 14:44:44 +00:00
|
|
|
end
|
|
|
|
end
|