From 9e59b19c16d098797ab4aa24bc6d892d84744536 Mon Sep 17 00:00:00 2001 From: Marcin Kulik Date: Sun, 4 Mar 2012 20:30:26 +0100 Subject: [PATCH] Claiming asciicasts --- app/controllers/user_tokens_controller.rb | 21 ++++++++ app/views/user_tokens/error.html.erb | 3 ++ config/routes.rb | 2 + .../user_tokens_controller_spec.rb | 49 +++++++++++++++++++ spec/factories/user_tokens.rb | 2 +- spec/models/user_token_spec.rb | 4 +- spec/routing/connect_spec.rb | 11 +++++ 7 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 app/controllers/user_tokens_controller.rb create mode 100644 app/views/user_tokens/error.html.erb create mode 100644 spec/controllers/user_tokens_controller_spec.rb create mode 100644 spec/routing/connect_spec.rb diff --git a/app/controllers/user_tokens_controller.rb b/app/controllers/user_tokens_controller.rb new file mode 100644 index 0000000..5af468d --- /dev/null +++ b/app/controllers/user_tokens_controller.rb @@ -0,0 +1,21 @@ +class UserTokensController < ApplicationController + # before_filter :ensure_authenticated! + + def create + ut = current_user.add_user_token(params[:user_token]) + + if ut.valid? + claimed_num = Asciicast.assign_user(ut.token, current_user) + + if claimed_num > 0 + notice = "Claimed #{claimed_num} asciicasts, yay!" + else + notice = "Authenticated successfully, yippie!" + end + + redirect_to root_path, :notice => notice + else + render :error + end + end +end diff --git a/app/views/user_tokens/error.html.erb b/app/views/user_tokens/error.html.erb new file mode 100644 index 0000000..44564d6 --- /dev/null +++ b/app/views/user_tokens/error.html.erb @@ -0,0 +1,3 @@ +

Oops.

+ +

Seems like the token is incorrect. Please make sure you've pasted it correctly.

diff --git a/config/routes.rb b/config/routes.rb index b7ed804..c504703 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -12,5 +12,7 @@ AsciiIo::Application.routes.draw do match "/login" => "sessions#new" match "/logout" => "sessions#destroy" + match "/connect/:user_token" => "user_tokens#create" + root :to => 'asciicasts#index' end diff --git a/spec/controllers/user_tokens_controller_spec.rb b/spec/controllers/user_tokens_controller_spec.rb new file mode 100644 index 0000000..1c50591 --- /dev/null +++ b/spec/controllers/user_tokens_controller_spec.rb @@ -0,0 +1,49 @@ +require 'spec_helper' + +describe UserTokensController do + describe '#create' do + let(:user) { Factory(:user) } + let(:user_token) { Factory.build(:user_token, :user => nil) } + + before do + @controller.stub!(:current_user => user) + user.stub!(:add_user_token => user_token) + end + + context 'when given token is valid' do + before do + user_token.stub!(:valid? => true) + end + + it 'calls Asciicast.assign_user' do + Asciicast.should_receive(:assign_user).with(user_token.token, user).and_return(1) + + post :create, :user_token => user_token.token + end + + it 'redirects to root_path' do + post :create, :user_token => user_token.token + + response.should redirect_to(root_path) + end + end + + context 'when given token is invalid' do + before do + user_token.stub!(:valid? => false) + end + + it 'calls Asciicast.assign_user' do + Asciicast.should_not_receive(:assign_user) + + post :create, :user_token => user_token.token + end + + it 'renders :error' do + post :create, :user_token => user_token.token + + response.should render_template(:error) + end + end + end +end diff --git a/spec/factories/user_tokens.rb b/spec/factories/user_tokens.rb index 0bbb494..b9826f5 100644 --- a/spec/factories/user_tokens.rb +++ b/spec/factories/user_tokens.rb @@ -3,6 +3,6 @@ FactoryGirl.define do factory :user_token do association :user - token "some-token" + sequence(:token) { |n| "2b4b4e02-6613-11e1-9be5-#{Kernel.format('%012i', n)}" } end end diff --git a/spec/models/user_token_spec.rb b/spec/models/user_token_spec.rb index a425ca4..8835028 100644 --- a/spec/models/user_token_spec.rb +++ b/spec/models/user_token_spec.rb @@ -1,5 +1,7 @@ require 'spec_helper' describe UserToken do - pending "add some examples to (or delete) #{__FILE__}" + it "has valid factory" do + Factory.build(:user_token).should be_valid + end end diff --git a/spec/routing/connect_spec.rb b/spec/routing/connect_spec.rb new file mode 100644 index 0000000..351f928 --- /dev/null +++ b/spec/routing/connect_spec.rb @@ -0,0 +1,11 @@ +require 'spec_helper' + +describe 'connect routing' do + it 'routes /connect/:user_token to user_tokens#create for user_token' do + { :get => '/connect/jolka-misio' }.should route_to( + :controller => 'user_tokens', + :action => 'create', + :user_token => 'jolka-misio' + ) + end +end