From 1ba90675516aa8908aa34ee005b671c92b08c7d2 Mon Sep 17 00:00:00 2001 From: Marcin Kulik Date: Sun, 4 Mar 2012 18:14:58 +0100 Subject: [PATCH] User tokens for assigning asciicasts to users --- app/models/asciicast.rb | 14 +++++ app/models/user.rb | 2 + app/models/user_token.rb | 5 ++ ...20304161025_add_user_token_to_asciicast.rb | 7 +++ .../20120304162005_create_user_tokens.rb | 13 +++++ db/schema.rb | 18 +++++-- spec/factories/asciicasts.rb | 19 +++---- spec/factories/user_tokens.rb | 8 +++ spec/factories/users.rb | 2 +- spec/fixtures/asciicasts/1/meta.json | 14 +++++ spec/fixtures/asciicasts/1/stdout | Bin 0 -> 272 bytes spec/fixtures/asciicasts/1/stdout.time | Bin 0 -> 199 bytes spec/models/asciicast_spec.rb | 50 +++++++++++++++++- spec/models/user_token_spec.rb | 5 ++ 14 files changed, 143 insertions(+), 14 deletions(-) create mode 100644 app/models/user_token.rb create mode 100644 db/migrate/20120304161025_add_user_token_to_asciicast.rb create mode 100644 db/migrate/20120304162005_create_user_tokens.rb create mode 100644 spec/factories/user_tokens.rb create mode 100644 spec/fixtures/asciicasts/1/meta.json create mode 100644 spec/fixtures/asciicasts/1/stdout create mode 100644 spec/fixtures/asciicasts/1/stdout.time create mode 100644 spec/models/user_token_spec.rb diff --git a/app/models/asciicast.rb b/app/models/asciicast.rb index ccbb7ac..58cb7bf 100644 --- a/app/models/asciicast.rb +++ b/app/models/asciicast.rb @@ -7,11 +7,16 @@ class Asciicast < ActiveRecord::Base validates :stdout, :stdout_timing, :presence => true validates :terminal_columns, :terminal_lines, :duration, :presence => true + belongs_to :user + + before_create :assign_user, :unless => :user + attr_reader :description def meta=(file) data = JSON.parse(file.tempfile.read) + self.user_token = data['user_token'] self.duration = data['duration'] self.recorded_at = data['recorded_at'] self.title = data['title'] @@ -45,4 +50,13 @@ class Asciicast < ActiveRecord::Base nil end end + + def assign_user + if user_token.present? + if ut = UserToken.find_by_token(user_token) + self.user = ut.user + self.user_token = nil + end + end + end end diff --git a/app/models/user.rb b/app/models/user.rb index 7c9c981..86c82f0 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -4,6 +4,8 @@ class User < ActiveRecord::Base validate :uid, :presence => true validate :nickname, :presence => true + has_many :user_tokens + def self.create_with_omniauth(auth) create! do |user| user.provider = auth["provider"] diff --git a/app/models/user_token.rb b/app/models/user_token.rb new file mode 100644 index 0000000..429f8f5 --- /dev/null +++ b/app/models/user_token.rb @@ -0,0 +1,5 @@ +class UserToken < ActiveRecord::Base + belongs_to :user + + validates :user, :token, :presence => true +end diff --git a/db/migrate/20120304161025_add_user_token_to_asciicast.rb b/db/migrate/20120304161025_add_user_token_to_asciicast.rb new file mode 100644 index 0000000..6ee80da --- /dev/null +++ b/db/migrate/20120304161025_add_user_token_to_asciicast.rb @@ -0,0 +1,7 @@ +class AddUserTokenToAsciicast < ActiveRecord::Migration + def change + add_column :asciicasts, :user_token, :string + + add_index :asciicasts, :user_token + end +end diff --git a/db/migrate/20120304162005_create_user_tokens.rb b/db/migrate/20120304162005_create_user_tokens.rb new file mode 100644 index 0000000..cfeecd0 --- /dev/null +++ b/db/migrate/20120304162005_create_user_tokens.rb @@ -0,0 +1,13 @@ +class CreateUserTokens < ActiveRecord::Migration + def change + create_table :user_tokens do |t| + t.integer :user_id, :null => false + t.string :token, :null => false + + t.timestamps + end + + add_index :user_tokens, :user_id + add_index :user_tokens, :token + end +end diff --git a/db/schema.rb b/db/schema.rb index 32ad090..fcbf5f6 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20120226184448) do +ActiveRecord::Schema.define(:version => 20120304162005) do create_table "asciicasts", :force => true do |t| t.integer "user_id" @@ -24,17 +24,29 @@ ActiveRecord::Schema.define(:version => 20120226184448) do t.string "command" t.string "shell" t.string "uname" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at" + t.datetime "updated_at" t.string "stdin" t.string "stdin_timing" t.string "stdout" t.string "stdout_timing" + t.string "user_token" end add_index "asciicasts", ["created_at"], :name => "index_asciicasts_on_created_at" add_index "asciicasts", ["recorded_at"], :name => "index_asciicasts_on_recorded_at" add_index "asciicasts", ["user_id"], :name => "index_asciicasts_on_user_id" + add_index "asciicasts", ["user_token"], :name => "index_asciicasts_on_user_token" + + create_table "user_tokens", :force => true do |t| + t.integer "user_id", :null => false + t.string "token", :null => false + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false + end + + add_index "user_tokens", ["token"], :name => "index_user_tokens_on_token" + add_index "user_tokens", ["user_id"], :name => "index_user_tokens_on_user_id" create_table "users", :force => true do |t| t.string "provider", :null => false diff --git a/spec/factories/asciicasts.rb b/spec/factories/asciicasts.rb index 4e6dc75..0aa0f2b 100644 --- a/spec/factories/asciicasts.rb +++ b/spec/factories/asciicasts.rb @@ -2,15 +2,16 @@ FactoryGirl.define do factory :asciicast do - user_id 1 - title "MyString" - duration 1 + association :user + title "bashing" + duration 100 recorded_at "2011-11-23 22:06:07" - terminal_type "MyString" - terminal_columns 1 - terminal_lines 1 - command "MyString" - shell "MyString" - uname "MyString" + terminal_type "xterm" + terminal_columns 80 + terminal_lines 25 + shell "/bin/bash" + uname "uname" + stdout { File.open('spec/fixtures/asciicasts/1/stdout') } + stdout_timing { File.open('spec/fixtures/asciicasts/1/stdout.time') } end end diff --git a/spec/factories/user_tokens.rb b/spec/factories/user_tokens.rb new file mode 100644 index 0000000..0bbb494 --- /dev/null +++ b/spec/factories/user_tokens.rb @@ -0,0 +1,8 @@ +# Read about factories at http://github.com/thoughtbot/factory_girl + +FactoryGirl.define do + factory :user_token do + association :user + token "some-token" + end +end diff --git a/spec/factories/users.rb b/spec/factories/users.rb index 0739cd2..a3e8f92 100644 --- a/spec/factories/users.rb +++ b/spec/factories/users.rb @@ -3,7 +3,7 @@ FactoryGirl.define do factory :user do provider "twitter" - uid "1234" + sequence(:uid) { |n| "uid-#{n}" } nickname "mrFoo" email nil name nil diff --git a/spec/fixtures/asciicasts/1/meta.json b/spec/fixtures/asciicasts/1/meta.json new file mode 100644 index 0000000..a8a9748 --- /dev/null +++ b/spec/fixtures/asciicasts/1/meta.json @@ -0,0 +1,14 @@ +{ + "command": null, + "duration": 5.914434194564819, + "recorded_at": "Sun, 04 Mar 2012 16:43:52 +0000", + "shell": "/bin/zsh", + "term": { + "columns": 89, + "lines": 27, + "type": "rxvt-unicode-256color" + }, + "title": null, + "uname": "Linux 3.2.6-3.fc16.x86_64 #1 SMP Mon Feb 13 20:35:42 UTC 2012 x86_64", + "user_token": "2b4b4e02-6613-11e1-9be5-00215c6bbb11" +} diff --git a/spec/fixtures/asciicasts/1/stdout b/spec/fixtures/asciicasts/1/stdout new file mode 100644 index 0000000000000000000000000000000000000000..d5e8e810bf7262b2e781b7279d864d9acee66a66 GIT binary patch literal 272 zcmV+r0q_1oT4*^jL0KkKS#JeE4*&oke}F*J00=^3{kVh}1PVWA-cc|CSS_p~r4K{~ zfB*mhJxmc&jRs8v)X2~P01W|BM2OQs(?A9Y&@uyL+FaDbT;Y<}SkYzi8BHcL*m0C{ zlwy%a<5{TBIf^(6#yAXyNk!x?DvdDup0fzTqIZ?LN0G45a>)LZx<~Aok9j@Dl82m| z3lr?A5pl*)p<@EY$jZ#B-!n;=+a^Jr^g^FTEL4DVVjV~jzOqxglZ4?0W<#k;oJv4v zbcMEN$PrQx#WJbg3kquuC=SD$a7Y&(U#<6ybA|bOTyuNr$!I0sz(oBrhifQfPQB zS~OyQSaT$2s4(t4(dx?On~o_Ajc{HCrFa+J=G&zPn$eUea(T|z*)z`O%R1*;i}kjX zV`h{_g3uZ@S*e(~^pT$%!qy|>VuHps_Mnz;&2?RJgtS*;?mEf*UC9*TLPJC(tyBlA BSl|Ev literal 0 HcmV?d00001 diff --git a/spec/models/asciicast_spec.rb b/spec/models/asciicast_spec.rb index e4fbbc5..cba5aad 100644 --- a/spec/models/asciicast_spec.rb +++ b/spec/models/asciicast_spec.rb @@ -1,5 +1,53 @@ require 'spec_helper' describe Asciicast do - pending "add some examples to (or delete) #{__FILE__}" + it "has valid factory" do + Factory.build(:asciicast).should be_valid + end + + describe '#save' do + let(:asciicast) { Factory.build(:asciicast, :user => user) } + + context 'when no user given' do + let(:user) { nil } + + it 'calls #assign_user' do + asciicast.should_receive(:assign_user) + asciicast.save + end + end + + context 'when user given' do + let(:user) { Factory.build(:user) } + + it "doesn't call #assign_user" do + asciicast.should_not_receive(:assign_user) + asciicast.save + end + end + end + + describe '#assign_user' do + let(:user) { Factory(:user) } + let(:asciicast) { Factory(:asciicast, :user => nil, :user_token => user_token) } + + context 'when user exists with given token' do + let(:user_token) { Factory(:user_token, :user => user).token } + + it 'assigns user and resets user_token' do + asciicast.assign_user + asciicast.user.should == user + asciicast.user_token.should be(nil) + end + end + + context 'when there is no user with given token' do + let(:user_token) { 'some-foo-bar' } + + it 'assigns user' do + asciicast.assign_user + asciicast.user.should be(nil) + end + end + end end diff --git a/spec/models/user_token_spec.rb b/spec/models/user_token_spec.rb new file mode 100644 index 0000000..a425ca4 --- /dev/null +++ b/spec/models/user_token_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe UserToken do + pending "add some examples to (or delete) #{__FILE__}" +end