Merge pull request #133 from travis-ci/ps-education

Check for user education status on signin/signup
This commit is contained in:
Piotr Sarnacki 2014-09-02 11:49:52 +02:00
commit b0684fa988
3 changed files with 37 additions and 9 deletions

View File

@ -37,7 +37,7 @@ GIT
GIT
remote: git://github.com/travis-ci/travis-core.git
revision: 0c4922f7cbf644ccc97677643bb09f26a919e64c
revision: c33c0d40306e2493d4b65ca00be6731a4e614bed
specs:
travis-core (0.0.1)
actionmailer (~> 3.2.19)

View File

@ -216,6 +216,10 @@ class Travis::Api::App
def info(attributes = {})
info = data.to_hash.slice('name', 'login', 'gravatar_id')
info.merge! attributes.stringify_keys
if Travis::Features.feature_active?(:education_data_sync) ||
(user && Travis::Features.owner_active?(:education_data_sync, user))
info['education'] = education
end
info['github_id'] ||= data['id']
info
end
@ -224,6 +228,10 @@ class Travis::Api::App
user
end
def education
Travis::Github::Education.new(token.to_s).student?
end
def fetch
retried ||= false
info = drop_token ? self.info : self.info(github_oauth_token: token)

View File

@ -3,6 +3,10 @@ require 'spec_helper'
describe Travis::Api::App::Endpoint::Authorization::UserManager do
let(:manager) { described_class.new(data, 'abc123') }
before do
Travis::Features.enable_for_all(:education_data_sync)
end
describe '#info' do
let(:data) {
{
@ -10,32 +14,35 @@ describe Travis::Api::App::Endpoint::Authorization::UserManager do
}.stringify_keys
}
before { manager.stubs(:education).returns(false) }
it 'gets data from github payload' do
manager.info.should == {
name: 'Piotr Sarnacki', login: 'drogus', gravatar_id: '123', github_id: 456
name: 'Piotr Sarnacki', login: 'drogus', gravatar_id: '123', github_id: 456, education: false
}.stringify_keys
end
it 'allows to overwrite existing keys' do
manager.info({login: 'piotr.sarnacki', bar: 'baz'}.stringify_keys).should == {
name: 'Piotr Sarnacki', login: 'piotr.sarnacki', gravatar_id: '123',
github_id: 456, bar: 'baz'
github_id: 456, bar: 'baz', education: false
}.stringify_keys
end
end
describe '#fetch' do
let(:data) {
{ login: 'drogus', id: 456 }.stringify_keys
}
let(:data) {
{ login: 'drogus', id: 456 }.stringify_keys
}
it 'drops the token when drop_token is set to true' do
user = stub('user', login: 'drogus', github_id: 456)
User.expects(:find_by_github_id).with(456).returns(user)
manager = described_class.new(data, 'abc123', true)
manager.stubs(:education).returns(false)
attributes = { login: 'drogus', github_id: 456 }.stringify_keys
attributes = { login: 'drogus', github_id: 456, education: false }.stringify_keys
user.expects(:update_attributes).with(attributes)
@ -46,8 +53,9 @@ describe Travis::Api::App::Endpoint::Authorization::UserManager do
it 'updates user data' do
user = stub('user', login: 'drogus', github_id: 456)
User.expects(:find_by_github_id).with(456).returns(user)
attributes = { login: 'drogus', github_id: 456, github_oauth_token: 'abc123' }.stringify_keys
attributes = { login: 'drogus', github_id: 456, github_oauth_token: 'abc123', education: false }.stringify_keys
user.expects(:update_attributes).with(attributes)
manager.stubs(:education).returns(false)
manager.fetch.should == user
end
@ -57,11 +65,23 @@ describe Travis::Api::App::Endpoint::Authorization::UserManager do
it 'creates new user' do
user = stub('user', login: 'drogus', github_id: 456)
User.expects(:find_by_github_id).with(456).returns(nil)
attributes = { login: 'drogus', github_id: 456, github_oauth_token: 'abc123' }.stringify_keys
attributes = { login: 'drogus', github_id: 456, github_oauth_token: 'abc123', education: false }.stringify_keys
User.expects(:create!).with(attributes).returns(user)
manager.stubs(:education).returns(false)
manager.fetch.should == user
end
end
end
describe '#education' do
let(:data) { {} }
it 'runs students check with token' do
education = stub(:education)
education.expects(:student?).returns(true)
Travis::Github::Education.expects(:new).with('abc123').returns(education)
manager.education.should be_truthy
end
end
end