Skip to content

Commit

Permalink
Store GitHub user data for v2 prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
Katrina Owen committed Aug 24, 2017
1 parent 372702a commit f961b18
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 16 deletions.
4 changes: 2 additions & 2 deletions lib/exercism/authentication.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Authentication
def self.perform(code, client_id, client_secret, target_profile)
id, username, email, avatar_url = Github.authenticate(code, client_id, client_secret)
User.from_github(id, username, email, avatar_url, target_profile)
id, username, email, avatar_url, bio, name = Github.authenticate(code, client_id, client_secret)
User.from_github(id, username, email, avatar_url, bio, name, target_profile)
end
end
2 changes: 1 addition & 1 deletion lib/exercism/github.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def self.authenticate(code, client_id, client_secret)
req.params['access_token'] = access_token
end
result = JSON.parse(response.body)
[result['id'], result['login'], result['email'], result['avatar_url']]
[result['id'], result['login'], result['email'], result['avatar_url'], result['bio'], result['name']]
end
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength

Expand Down
6 changes: 4 additions & 2 deletions lib/exercism/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def access?(problem)
end

# rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
def self.from_github(id, username, email, avatar_url, joined_as=nil)
def self.from_github(id, username, email, avatar_url, bio, name, joined_as=nil)
user = User.where(github_id: id).first
# try to match an invitation that has been sent.
# GitHub ID will only be nil if the user has never logged in.
Expand All @@ -61,8 +61,10 @@ def self.from_github(id, username, email, avatar_url, joined_as=nil)
user.joined_as = joined_as if user.joined_as.nil? && !!joined_as

user.github_id = id
user.email = email unless user.email
user.email = email
user.username = username
user.name = name
user.bio = bio
user.avatar_url = avatar_url.gsub(/\?.+$/, '') if avatar_url
user.save

Expand Down
23 changes: 12 additions & 11 deletions test/exercism/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,54 +19,55 @@ def test_flipper_id_is_username
end

def test_create_user_from_github
user = User.from_github(23, 'alice', '[email protected]', 'avatar_url', 'polyglot')
user = User.from_github(23, 'alice', '[email protected]', 'avatar_url', 'I am Alice', 'Alice Jones', 'polyglot')
assert_equal 1, User.count
assert_equal 23, user.github_id
assert_equal 'alice', user.username
assert_equal '[email protected]', user.email
assert_equal 'avatar_url', user.avatar_url
assert_equal 'polyglot', user.joined_as
assert_equal 'I am Alice', user.bio
assert_equal 'Alice Jones', user.name
end

def test_update_username_from_github
User.create(github_id: 23)
user = User.from_github(23, 'bob', nil, nil).reload
user = User.from_github(23, 'bob', nil, nil, nil, nil).reload
assert_equal 'bob', user.username
end

def test_does_not_overwrite_email_or_joined_as_if_present
User.create(github_id: 23, email: '[email protected]', joined_as: 'polyglot')
user = User.from_github(23, nil, '[email protected]', nil, 'artisan').reload
assert_equal '[email protected]', user.email
def test_does_not_overwrite_joined_as_if_present
User.create(github_id: 23, joined_as: 'polyglot')
user = User.from_github(23, nil, nil, nil, nil, nil, 'artisan').reload
assert_equal 'polyglot', user.joined_as
end

def test_sets_avatar_url
User.create(github_id: 23)
user = User.from_github(23, nil, nil, 'new?1234').reload
user = User.from_github(23, nil, nil, 'new?1234', nil, nil).reload
assert_equal 'new', user.avatar_url
end

def test_overwrites_avatar_url_if_present
User.create(github_id: 23, avatar_url: 'old')
user = User.from_github(23, nil, nil, 'new?1234').reload
user = User.from_github(23, nil, nil, 'new?1234', nil, nil).reload
assert_equal 'new', user.avatar_url
end

def test_from_github_unsets_old_duplicate_username
u1 = User.create(github_id: 23, username: 'alice')
u2 = User.from_github(31, 'alice', nil, nil).reload
u2 = User.from_github(31, 'alice', nil, nil, nil, nil).reload
assert_equal 'alice', u2.username
assert_equal '', u1.reload.username

# it doesn't overwrite it's own username next time
u3 = User.from_github(31, 'alice', nil, nil).reload
u3 = User.from_github(31, 'alice', nil, nil, nil, nil).reload
assert_equal 'alice', u3.username
end

def test_from_github_connects_invited_user
u1 = User.create(username: 'alice')
u2 = User.from_github(42, 'alice', '[email protected]', 'avatar').reload
u2 = User.from_github(42, 'alice', '[email protected]', 'avatar', nil, nil).reload

u1.reload

Expand Down

0 comments on commit f961b18

Please sign in to comment.