Check for user education status on signin/signup

This commit is contained in:
Piotr Sarnacki 2014-08-28 17:18:07 +02:00
parent 4546d29508
commit 9b07efa5e9
4 changed files with 32 additions and 10 deletions

View File

@ -3,7 +3,7 @@ ruby '2.1.2'
source 'https://rubygems.org'
gemspec
gem 'travis-core', github: 'travis-ci/travis-core'
gem 'travis-core', github: 'travis-ci/travis-core', branch: 'ps-education-sync'
gem 'travis-support', github: 'travis-ci/travis-support'
gem 'travis-sidekiqs', github: 'travis-ci/travis-sidekiqs', require: nil, ref: 'cde9741'
gem 'travis-yaml', github: 'travis-ci/travis-yaml'

View File

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

View File

@ -216,6 +216,7 @@ class Travis::Api::App
def info(attributes = {})
info = data.to_hash.slice('name', 'login', 'gravatar_id')
info.merge! attributes.stringify_keys
info['education'] = education
info['github_id'] ||= data['id']
info
end
@ -224,6 +225,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

@ -10,32 +10,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 +49,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 +61,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