From b3659256fcc2c4bbf8d9b35c55dc01929dae75c0 Mon Sep 17 00:00:00 2001 From: Benjamin Fleischer Date: Sun, 23 Jun 2013 20:14:22 -0500 Subject: [PATCH 1/7] BF/RB Get current user by github login, not a github object Benjamin Fleischer Rich Blumer --- lib/ppwm_matcher/app.rb | 4 ++-- lib/ppwm_matcher/models/user.rb | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/ppwm_matcher/app.rb b/lib/ppwm_matcher/app.rb index cdba4dc..69354f2 100644 --- a/lib/ppwm_matcher/app.rb +++ b/lib/ppwm_matcher/app.rb @@ -99,7 +99,7 @@ def authorized? 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 +136,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) From 28835b82850194f3d3df024823e7b8580cd77eea Mon Sep 17 00:00:00 2001 From: Benjamin Fleischer Date: Sun, 23 Jun 2013 20:30:54 -0500 Subject: [PATCH 2/7] BF/RB Adding user profile route /profile/:github_login If user is registered, show profile else show 'no such user' page --- lib/ppwm_matcher/app.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/ppwm_matcher/app.rb b/lib/ppwm_matcher/app.rb index 69354f2..b713ec8 100644 --- a/lib/ppwm_matcher/app.rb +++ b/lib/ppwm_matcher/app.rb @@ -98,6 +98,16 @@ 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 + "Hello #{github_login}" + else + "No such user" + end + end get '/code' do user = User.current(github_user.login) # TODO: refactor to helper method ? redirect '/' unless user && user.code From b9177944471cffec3423891033c4d514b9d88443 Mon Sep 17 00:00:00 2001 From: Jon Cairns Date: Fri, 28 Jun 2013 13:29:35 +0100 Subject: [PATCH 3/7] Fix unescaped dash in github profile regex --- lib/ppwm_matcher/app.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ppwm_matcher/app.rb b/lib/ppwm_matcher/app.rb index b713ec8..6468f8c 100644 --- a/lib/ppwm_matcher/app.rb +++ b/lib/ppwm_matcher/app.rb @@ -100,7 +100,7 @@ def authorized? get '/profile/:github_login' do # TODO revisit how to ensure we get a safe string - github_login = params[:github_login].to_s.gsub(/[\s-\/\\\.]/, '') + github_login = params[:github_login].to_s.gsub(/[\s\-\/\\\.]/, '') user = User.current(github_login) if user "Hello #{github_login}" From 389c59414c4bce84849d31e0502d0058c7cdcbed Mon Sep 17 00:00:00 2001 From: Benjamin Fleischer Date: Sun, 30 Jun 2013 21:44:38 -0500 Subject: [PATCH 4/7] BF/RB Add test for viewing user profile --- spec/app_spec.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/spec/app_spec.rb b/spec/app_spec.rb index f839d5d..1804471 100644 --- a/spec/app_spec.rb +++ b/spec/app_spec.rb @@ -27,6 +27,26 @@ 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 + + PpwmMatcher::User.update_or_create('foo@example.com', github_user) + get "/profile/#{github_user.login}" + + expect(last_response.body).to include("Hello #{github_user.login}") + 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' From 84a6fa218836f5c57da08b79195d283f7dc427f8 Mon Sep 17 00:00:00 2001 From: Benjamin Fleischer Date: Sun, 30 Jun 2013 22:07:01 -0500 Subject: [PATCH 5/7] BF/RB Surface more profile details and use layout --- lib/ppwm_matcher/app.rb | 6 +++--- lib/ppwm_matcher/views/profile.erb | 7 +++++++ spec/app_spec.rb | 9 ++++++--- 3 files changed, 16 insertions(+), 6 deletions(-) create mode 100644 lib/ppwm_matcher/views/profile.erb diff --git a/lib/ppwm_matcher/app.rb b/lib/ppwm_matcher/app.rb index 6468f8c..432ada0 100644 --- a/lib/ppwm_matcher/app.rb +++ b/lib/ppwm_matcher/app.rb @@ -101,9 +101,9 @@ def authorized? 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 - "Hello #{github_login}" + @user = User.current(github_login) + if @user + erb :profile, layout: :layout else "No such user" end 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 +
+
    +
  • Email : <%= @user.email %>
  • +
  • Github user : <%= @user.github_login %>
  • +
  • Gravatar
  • +
diff --git a/spec/app_spec.rb b/spec/app_spec.rb index 1804471..3d5950e 100644 --- a/spec/app_spec.rb +++ b/spec/app_spec.rb @@ -32,10 +32,13 @@ def app it 'if the user exists, says Hello github_login' do login_as github_user - PpwmMatcher::User.update_or_create('foo@example.com', github_user) - get "/profile/#{github_user.login}" + 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 - expect(last_response.body).to include("Hello #{github_user.login}") end it 'if user does not exists,says No such user' do login_as github_user From 666bc498986a122be515795abecd2e172ef44715 Mon Sep 17 00:00:00 2001 From: Benjamin Fleischer Date: Sun, 30 Jun 2013 22:16:45 -0500 Subject: [PATCH 6/7] BF/RB make rspec optional when running rake tasks --- Rakefile | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) 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" From 234e6f5d697a13e5b4c5317bdda46fbcf56e7ae2 Mon Sep 17 00:00:00 2001 From: Benjamin Fleischer Date: Sun, 30 Jun 2013 22:22:18 -0500 Subject: [PATCH 7/7] BF/RB make thin available in non-mounted deploys --- Gemfile | 2 ++ ppwm-matcher.gemspec | 1 - 2 files changed, 2 insertions(+), 1 deletion(-) 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/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'