diff --git a/Gemfile b/Gemfile index fa75df1..61f7d22 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,5 @@ source 'https://rubygems.org' gemspec + +gem 'thin' diff --git a/Rakefile b/Rakefile index 0803fff..2f215a7 100644 --- a/Rakefile +++ b/Rakefile @@ -1,11 +1,15 @@ require './app' require 'sinatra/activerecord/rake' -require 'rspec/core/rake_task' +begin + require 'rspec/core/rake_task' -RSpec::Core::RakeTask.new(:spec) + RSpec::Core::RakeTask.new(:spec) -task :default => :spec + task :default => :spec +rescue LoadError + puts "We don't have rspec" +end namespace :db do desc "Populate the database with example data" diff --git a/lib/ppwm_matcher/app.rb b/lib/ppwm_matcher/app.rb index cdba4dc..432ada0 100644 --- a/lib/ppwm_matcher/app.rb +++ b/lib/ppwm_matcher/app.rb @@ -98,8 +98,18 @@ def authorized? end end + get '/profile/:github_login' do + # TODO revisit how to ensure we get a safe string + github_login = params[:github_login].to_s.gsub(/[\s\-\/\\\.]/, '') + @user = User.current(github_login) + if @user + erb :profile, layout: :layout + else + "No such user" + end + end get '/code' do - user = User.current(github_user) # TODO: refactor to helper method ? + user = User.current(github_user.login) # TODO: refactor to helper method ? redirect '/' unless user && user.code @pair = user.pair @@ -136,7 +146,7 @@ def setup_for_root_path(messages = nil) @email = params['email'] || github_user.email @name = github_user.name || github_user.login - user = User.current(github_user) + user = User.current(github_user.login) if user && user.has_code? @has_code = true end diff --git a/lib/ppwm_matcher/models/user.rb b/lib/ppwm_matcher/models/user.rb index d0d1fef..c603c4c 100644 --- a/lib/ppwm_matcher/models/user.rb +++ b/lib/ppwm_matcher/models/user.rb @@ -21,8 +21,8 @@ def pair User.where(:code_id => self.code_id).detect{|u| u != self } end - def self.current(github_user) - where(:github_login => github_user.login).limit(1).first + def self.current(github_login) + where(:github_login => github_login).limit(1).first end def self.update_or_create(email, github_user) diff --git a/lib/ppwm_matcher/views/profile.erb b/lib/ppwm_matcher/views/profile.erb new file mode 100644 index 0000000..00deabd --- /dev/null +++ b/lib/ppwm_matcher/views/profile.erb @@ -0,0 +1,7 @@ +<%= @user.name %>'s Profile +
+ diff --git a/ppwm-matcher.gemspec b/ppwm-matcher.gemspec index 73b1ef7..fbd244d 100644 --- a/ppwm-matcher.gemspec +++ b/ppwm-matcher.gemspec @@ -16,7 +16,6 @@ Gem::Specification.new do |s| s.add_dependency 'rake' s.add_dependency 'pony' - s.add_development_dependency 'thin' s.add_development_dependency 'rspec' s.add_development_dependency 'faker' s.add_development_dependency 'factory_girl' diff --git a/spec/app_spec.rb b/spec/app_spec.rb index f839d5d..3d5950e 100644 --- a/spec/app_spec.rb +++ b/spec/app_spec.rb @@ -27,6 +27,29 @@ def app end end + describe "GET /profile/:github_login" do + context 'the user is authorized' do + it 'if the user exists, says Hello github_login' do + login_as github_user + + user = PpwmMatcher::User.update_or_create('foo@example.com', github_user) + get "/profile/#{user.github_login}" + + [:github_login, :name, :email, :gravatar_id].each do |profile_attribute| + expect(last_response.body).to include(user.public_send(profile_attribute)) + end + + end + it 'if user does not exists,says No such user' do + login_as github_user + + get "/profile/#{github_user.login}" + + expect(last_response.body).to include('No such user') + end + end + end + describe "GET /unauthenticated" do it "should not redirect for authentication" do get '/unauthenticated'