redis-friendships 0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +3 -0
- data/Gemfile +2 -0
- data/LICENSE +23 -0
- data/README.rdoc +51 -0
- data/Rakefile +10 -0
- data/features/redis_friendships_named.feature +60 -0
- data/features/redis_friendships_standard.feature +59 -0
- data/features/redis_friendships_user_injection.feature +37 -0
- data/features/step_definitions/common_steps.rb +47 -0
- data/features/step_definitions/rails_setup_steps.rb +6 -0
- data/features/support/env.rb +6 -0
- data/features/support/matchers.rb +7 -0
- data/lib/generators/redis_friendships/USAGE +53 -0
- data/lib/generators/redis_friendships/redis_friendships_generator.rb +90 -0
- data/lib/generators/redis_friendships/templates/friendship.rb +29 -0
- data/lib/generators/redis_friendships/templates/redis.rb +1 -0
- data/lib/generators/redis_friendships/templates/tests/rspec/sessions_controller.rb +39 -0
- data/lib/generators/redis_friendships/templates/tests/rspec/user.rb +83 -0
- data/lib/generators/redis_friendships/templates/tests/rspec/users_controller.rb +56 -0
- data/lib/generators/redis_friendships/templates/user.rb +32 -0
- data/lib/generators/redis_friendships/templates/user_injection_code.rb +28 -0
- data/lib/generators/redis_friendships/templates/user_migration.rb +15 -0
- data/test/test_helper.rb +119 -0
- data/test/test_nifty_authentication_generator.rb +274 -0
- data/test/test_nifty_config_generator.rb +37 -0
- data/test/test_nifty_layout_generator.rb +42 -0
- metadata +179 -0
@@ -0,0 +1,29 @@
|
|
1
|
+
class <%= friendship_class_name %>
|
2
|
+
|
3
|
+
def self.follow!(username, followed_username)
|
4
|
+
<%=redis_constant_name%>.sadd("<%=friendship_singular_name%>:#{username}:followings", followed_username)
|
5
|
+
<%=redis_constant_name%>.sadd("<%=friendship_singular_name%>:#{followed_username}:followings", username)
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.stop_following!(username, followed_username)
|
9
|
+
<%=redis_constant_name%>.srem("<%=friendship_singular_name%>:#{username}:followings", followed_username)
|
10
|
+
<%=redis_constant_name%>.srem("<%=friendship_singular_name%>:#{followed_username}:followings", username)
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.follows?(username, followed_username)
|
14
|
+
<%=redis_constant_name%>.sismember("<%=friendship_singular_name%>:#{username}:followings", followed_username)
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.followed_by?(user, following_user)
|
18
|
+
<%=redis_constant_name%>.sismember("<%=friendship_singular_name%>:#{username}:followers", following_username)
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.followings_for(username)
|
22
|
+
<%= redis_constant_name %>.smembers("<%= friendship_singular_name %>:#{username}:followings")
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.followers_for(username)
|
26
|
+
<%= redis_constant_name %>.smembers("<%= friendship_singular_name %>:#{username}:followers")
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
<%= redis_constant_name %> = Redis.new
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
describe <%= session_plural_class_name %>Controller do
|
4
|
+
fixtures :all
|
5
|
+
render_views
|
6
|
+
|
7
|
+
it "new action should render new template" do
|
8
|
+
get :new
|
9
|
+
response.should render_template(:new)
|
10
|
+
end
|
11
|
+
|
12
|
+
<%- if options[:authlogic] -%>
|
13
|
+
it "create action should render new template when authentication is invalid" do
|
14
|
+
post :create, :<%= session_singular_name %> => { :username => "foo", :password => "badpassword" }
|
15
|
+
response.should render_template(:new)
|
16
|
+
<%= session_class_name %>.find.should be_nil
|
17
|
+
end
|
18
|
+
|
19
|
+
it "create action should redirect when authentication is valid" do
|
20
|
+
post :create, :<%= session_singular_name %> => { :username => "foo", :password => "secret" }
|
21
|
+
response.should redirect_to("/")
|
22
|
+
<%= session_class_name %>.find.<%= user_singular_name %>.should == <%= user_plural_name %>(:foo)
|
23
|
+
end
|
24
|
+
<%- else -%>
|
25
|
+
it "create action should render new template when authentication is invalid" do
|
26
|
+
<%= user_class_name %>.stubs(:authenticate).returns(nil)
|
27
|
+
post :create
|
28
|
+
response.should render_template(:new)
|
29
|
+
session['<%= user_singular_name %>_id'].should be_nil
|
30
|
+
end
|
31
|
+
|
32
|
+
it "create action should redirect when authentication is valid" do
|
33
|
+
<%= user_class_name %>.stubs(:authenticate).returns(<%= user_class_name %>.first)
|
34
|
+
post :create
|
35
|
+
response.should redirect_to("/")
|
36
|
+
session['<%= user_singular_name %>_id'].should == <%= user_class_name %>.first.id
|
37
|
+
end
|
38
|
+
<%- end -%>
|
39
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
describe <%= user_class_name %> do
|
4
|
+
<%- unless options[:authlogic] -%>
|
5
|
+
def new_<%= user_singular_name %>(attributes = {})
|
6
|
+
attributes[:username] ||= 'foo'
|
7
|
+
attributes[:email] ||= 'foo@example.com'
|
8
|
+
attributes[:password] ||= 'abc123'
|
9
|
+
attributes[:password_confirmation] ||= attributes[:password]
|
10
|
+
<%= user_class_name %>.new(attributes)
|
11
|
+
end
|
12
|
+
|
13
|
+
before(:each) do
|
14
|
+
<%= user_class_name %>.delete_all
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should be valid" do
|
18
|
+
new_<%= user_singular_name %>.should be_valid
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should require username" do
|
22
|
+
new_<%= user_singular_name %>(:username => '').should have(1).error_on(:username)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should require password" do
|
26
|
+
new_<%= user_singular_name %>(:password => '').should have(1).error_on(:password)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should require well formed email" do
|
30
|
+
new_<%= user_singular_name %>(:email => 'foo@bar@example.com').should have(1).error_on(:email)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should validate uniqueness of email" do
|
34
|
+
new_<%= user_singular_name %>(:email => 'bar@example.com').save!
|
35
|
+
new_<%= user_singular_name %>(:email => 'bar@example.com').should have(1).error_on(:email)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should validate uniqueness of username" do
|
39
|
+
new_<%= user_singular_name %>(:username => 'uniquename').save!
|
40
|
+
new_<%= user_singular_name %>(:username => 'uniquename').should have(1).error_on(:username)
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should not allow odd characters in username" do
|
44
|
+
new_<%= user_singular_name %>(:username => 'odd ^&(@)').should have(1).error_on(:username)
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should validate password is longer than 3 characters" do
|
48
|
+
new_<%= user_singular_name %>(:password => 'bad').should have(1).error_on(:password)
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should require matching password confirmation" do
|
52
|
+
new_<%= user_singular_name %>(:password_confirmation => 'nonmatching').should have(1).error_on(:password)
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should generate password hash and salt on create" do
|
56
|
+
<%= user_singular_name %> = new_<%= user_singular_name %>
|
57
|
+
<%= user_singular_name %>.save!
|
58
|
+
<%= user_singular_name %>.password_hash.should_not be_nil
|
59
|
+
<%= user_singular_name %>.password_salt.should_not be_nil
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should authenticate by username" do
|
63
|
+
<%= user_singular_name %> = new_<%= user_singular_name %>(:username => 'foobar', :password => 'secret')
|
64
|
+
<%= user_singular_name %>.save!
|
65
|
+
<%= user_class_name %>.authenticate('foobar', 'secret').should == <%= user_singular_name %>
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should authenticate by email" do
|
69
|
+
<%= user_singular_name %> = new_<%= user_singular_name %>(:email => 'foo@bar.com', :password => 'secret')
|
70
|
+
<%= user_singular_name %>.save!
|
71
|
+
<%= user_class_name %>.authenticate('foo@bar.com', 'secret').should == <%= user_singular_name %>
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should not authenticate bad username" do
|
75
|
+
<%= user_class_name %>.authenticate('nonexisting', 'secret').should be_nil
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should not authenticate bad password" do
|
79
|
+
new_<%= user_singular_name %>(:username => 'foobar', :password => 'secret').save!
|
80
|
+
<%= user_class_name %>.authenticate('foobar', 'badpassword').should be_nil
|
81
|
+
end
|
82
|
+
<%- end -%>
|
83
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
describe <%= user_plural_class_name %>Controller do
|
4
|
+
fixtures :all
|
5
|
+
render_views
|
6
|
+
|
7
|
+
it "new action should render new template" do
|
8
|
+
get :new
|
9
|
+
response.should render_template(:new)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "create action should render new template when model is invalid" do
|
13
|
+
<%= user_class_name %>.any_instance.stubs(:valid?).returns(false)
|
14
|
+
post :create
|
15
|
+
response.should render_template(:new)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "create action should redirect when model is valid" do
|
19
|
+
<%= user_class_name %>.any_instance.stubs(:valid?).returns(true)
|
20
|
+
post :create
|
21
|
+
response.should redirect_to("/")
|
22
|
+
<%- unless options[:authlogic] -%>
|
23
|
+
session['<%= user_singular_name %>_id'].should == assigns['<%= user_singular_name %>'].id
|
24
|
+
<%- end -%>
|
25
|
+
end
|
26
|
+
|
27
|
+
it "edit action should redirect when not logged in" do
|
28
|
+
get :edit, :id => "ignored"
|
29
|
+
response.should redirect_to(login_url)
|
30
|
+
end
|
31
|
+
|
32
|
+
it "edit action should render edit template" do
|
33
|
+
@controller.stubs(:current_<%= user_singular_name %>).returns(<%= user_class_name %>.first)
|
34
|
+
get :edit, :id => "ignored"
|
35
|
+
response.should render_template(:edit)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "update action should redirect when not logged in" do
|
39
|
+
put :update, :id => "ignored"
|
40
|
+
response.should redirect_to(login_url)
|
41
|
+
end
|
42
|
+
|
43
|
+
it "update action should render edit template when <%= user_singular_name %> is invalid" do
|
44
|
+
@controller.stubs(:current_<%= user_singular_name %>).returns(<%= user_class_name %>.first)
|
45
|
+
<%= user_class_name %>.any_instance.stubs(:valid?).returns(false)
|
46
|
+
put :update, :id => "ignored"
|
47
|
+
response.should render_template(:edit)
|
48
|
+
end
|
49
|
+
|
50
|
+
it "update action should redirect when <%= user_singular_name %> is valid" do
|
51
|
+
@controller.stubs(:current_<%= user_singular_name %>).returns(<%= user_class_name %>.first)
|
52
|
+
<%= user_class_name %>.any_instance.stubs(:valid?).returns(true)
|
53
|
+
put :update, :id => "ignored"
|
54
|
+
response.should redirect_to("/")
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
class <%= user_class_name %> < ActiveRecord::Base
|
2
|
+
|
3
|
+
def followed_by?(user)
|
4
|
+
<%=friendship_class_name%>.followed_by?(self.username, user.username)
|
5
|
+
end
|
6
|
+
|
7
|
+
def follows?(user)
|
8
|
+
<%=friendship_class_name%>.follows?(self.username, user.username)
|
9
|
+
end
|
10
|
+
|
11
|
+
def follow!(user)
|
12
|
+
return false if self.username == user.username
|
13
|
+
<%=friendship_class_name%>.follow!(self.username, user.username)
|
14
|
+
true
|
15
|
+
end
|
16
|
+
|
17
|
+
def stop_following!(user)
|
18
|
+
<%=friendship_class_name%>.stop_following!(self.username, user.username)
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
def followers
|
23
|
+
followers = <%= friendship_class_name %>.followers_for(self.username)
|
24
|
+
<%= user_class_name %>.where(:username => followers)
|
25
|
+
end
|
26
|
+
|
27
|
+
def followings
|
28
|
+
followings = <%= friendship_class_name %>.followings_for(self.username)
|
29
|
+
<%= user_class_name %>.where(:username => followings)
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
def followed_by?(user)
|
2
|
+
<%=friendship_class_name%>.followed_by?(self.username, user.username)
|
3
|
+
end
|
4
|
+
|
5
|
+
def follows?(user)
|
6
|
+
<%=friendship_class_name%>.follows?(self.username, user.username)
|
7
|
+
end
|
8
|
+
|
9
|
+
def follow!(user)
|
10
|
+
return false if self.username == user.username
|
11
|
+
<%=friendship_class_name%>.follow!(self.username, user.username)
|
12
|
+
true
|
13
|
+
end
|
14
|
+
|
15
|
+
def stop_following!(user)
|
16
|
+
<%=friendship_class_name%>.stop_following!(self.username, user.username)
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
def followers
|
21
|
+
followers = <%= friendship_class_name %>.followers_for(self.username)
|
22
|
+
<%= user_class_name %>.where(:username => followers)
|
23
|
+
end
|
24
|
+
|
25
|
+
def followings
|
26
|
+
followings = <%= friendship_class_name %>.followings_for(self.username)
|
27
|
+
<%= user_class_name %>.where(:username => followings)
|
28
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class Create<%= user_plural_class_name %> < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table :<%= user_plural_name %> do |t|
|
4
|
+
t.string :username
|
5
|
+
t.string :email
|
6
|
+
t.string :password_hash
|
7
|
+
t.string :password_salt
|
8
|
+
t.timestamps
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.down
|
13
|
+
drop_table :<%= user_plural_name %>
|
14
|
+
end
|
15
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
|
3
|
+
# Must set before requiring generator libs.
|
4
|
+
TMP_ROOT = File.dirname(__FILE__) + "/tmp" unless defined?(TMP_ROOT)
|
5
|
+
PROJECT_NAME = "myproject" unless defined?(PROJECT_NAME)
|
6
|
+
app_root = File.join(TMP_ROOT, PROJECT_NAME)
|
7
|
+
if defined?(APP_ROOT)
|
8
|
+
APP_ROOT.replace(app_root)
|
9
|
+
else
|
10
|
+
APP_ROOT = app_root
|
11
|
+
end
|
12
|
+
if defined?(RAILS_ROOT)
|
13
|
+
RAILS_ROOT.replace(app_root)
|
14
|
+
else
|
15
|
+
RAILS_ROOT = app_root
|
16
|
+
end
|
17
|
+
|
18
|
+
require 'rubygems'
|
19
|
+
gem 'rails', '2.0.2' # getting a Rails.configuration error with 2.1
|
20
|
+
gem 'rubigen', '1.4'
|
21
|
+
gem 'shoulda', '2.0.6'
|
22
|
+
require 'rubigen' # gem install rubigen --version=1.4
|
23
|
+
require 'rubigen/helpers/generator_test_helper'
|
24
|
+
require 'rails_generator'
|
25
|
+
require 'shoulda' # gem install shoulda --version=2.0.6
|
26
|
+
require 'mocha'
|
27
|
+
|
28
|
+
module RedisMessages
|
29
|
+
module TestHelper
|
30
|
+
include RubiGen::GeneratorTestHelper
|
31
|
+
|
32
|
+
def setup
|
33
|
+
bare_setup
|
34
|
+
end
|
35
|
+
|
36
|
+
def teardown
|
37
|
+
bare_teardown
|
38
|
+
end
|
39
|
+
|
40
|
+
protected
|
41
|
+
|
42
|
+
def run_rails_generator(generator, *args)
|
43
|
+
options = args.pop if args.last.kind_of? Hash
|
44
|
+
options ||= {}
|
45
|
+
run_generator(generator.to_s, args, generator_sources, options.reverse_merge(:quiet => true))
|
46
|
+
end
|
47
|
+
|
48
|
+
def generator_sources
|
49
|
+
[RubiGen::PathSource.new(:test, File.join(File.dirname(__FILE__), "..", "rails_generators"))]
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
module ShouldaAdditions
|
54
|
+
def rails_generator(*args)
|
55
|
+
setup do
|
56
|
+
run_rails_generator(*args)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def should_generate_file(file, &block)
|
61
|
+
should "generate file #{file}" do
|
62
|
+
yield("foo") if block_given?
|
63
|
+
assert_generated_file(file)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def should_not_generate_file(file)
|
68
|
+
should "not generate file #{file}" do
|
69
|
+
assert !File.exists?("#{APP_ROOT}/#{file}"),"The file '#{file}' should not exist"
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
class Thoughtbot::Shoulda::Context
|
76
|
+
include RedisMessages::ShouldaAdditions
|
77
|
+
end
|
78
|
+
|
79
|
+
# Mock out what we need from AR::Base.
|
80
|
+
module ActiveRecord
|
81
|
+
class Base
|
82
|
+
class << self
|
83
|
+
attr_accessor :pluralize_table_names, :columns
|
84
|
+
|
85
|
+
def add_column(name, type = :string)
|
86
|
+
returning ActiveRecord::ConnectionAdapters::Column.new(name, nil) do |column|
|
87
|
+
column.type = type
|
88
|
+
@columns ||= []
|
89
|
+
@columns << column
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
self.pluralize_table_names = true
|
94
|
+
end
|
95
|
+
|
96
|
+
module ConnectionAdapters
|
97
|
+
class Column
|
98
|
+
attr_accessor :name, :default, :type, :limit, :null, :sql_type, :precision, :scale
|
99
|
+
|
100
|
+
def initialize(name, default, sql_type = nil)
|
101
|
+
@name = name
|
102
|
+
@default = default
|
103
|
+
@type = @sql_type = sql_type
|
104
|
+
end
|
105
|
+
|
106
|
+
def human_name
|
107
|
+
@name.humanize
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
# And what we need from ActionView
|
114
|
+
module ActionView
|
115
|
+
module Helpers
|
116
|
+
module ActiveRecordHelper; end
|
117
|
+
class InstanceTag; end
|
118
|
+
end
|
119
|
+
end
|
@@ -0,0 +1,274 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "test_helper.rb")
|
2
|
+
|
3
|
+
class TestSeivanAuthenticationGenerator < Test::Unit::TestCase
|
4
|
+
include RedisMessages::TestHelper
|
5
|
+
|
6
|
+
# Some generator-related assertions:
|
7
|
+
# assert_generated_file(name, &block) # block passed the file contents
|
8
|
+
# assert_directory_exists(name)
|
9
|
+
# assert_generated_class(name, &block)
|
10
|
+
# assert_generated_module(name, &block)
|
11
|
+
# assert_generated_test_for(name, &block)
|
12
|
+
# The assert_generated_(class|module|test_for) &block is passed the body of the class/module within the file
|
13
|
+
# assert_has_method(body, *methods) # check that the body has a list of methods (methods with parentheses not supported yet)
|
14
|
+
#
|
15
|
+
# Other helper methods are:
|
16
|
+
# app_root_files - put this in teardown to show files generated by the test method (e.g. p app_root_files)
|
17
|
+
# bare_setup - place this in setup method to create the APP_ROOT folder for each test
|
18
|
+
# bare_teardown - place this in teardown method to destroy the TMP_ROOT or APP_ROOT folder after each test
|
19
|
+
context "" do # empty context so we can use setup block
|
20
|
+
setup do
|
21
|
+
Rails.stubs(:version).returns("2.0.2")
|
22
|
+
Dir.mkdir("#{RAILS_ROOT}/config") unless File.exists?("#{RAILS_ROOT}/config")
|
23
|
+
File.open("#{RAILS_ROOT}/config/routes.rb", 'w') do |f|
|
24
|
+
f.puts "ActionController::Routing::Routes.draw do |map|\n\nend"
|
25
|
+
end
|
26
|
+
Dir.mkdir("#{RAILS_ROOT}/app") unless File.exists?("#{RAILS_ROOT}/app")
|
27
|
+
Dir.mkdir("#{RAILS_ROOT}/app/controllers") unless File.exists?("#{RAILS_ROOT}/app/controllers")
|
28
|
+
File.open("#{RAILS_ROOT}/app/controllers/application.rb", 'w') do |f|
|
29
|
+
f.puts "class Application < ActionController::Base\n\nend"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
teardown do
|
34
|
+
FileUtils.rm_rf "#{RAILS_ROOT}/config"
|
35
|
+
FileUtils.rm_rf "#{RAILS_ROOT}/app"
|
36
|
+
end
|
37
|
+
|
38
|
+
context "generator without arguments" do
|
39
|
+
rails_generator :nifty_authentication
|
40
|
+
should_generate_file 'app/models/user.rb'
|
41
|
+
should_generate_file 'app/controllers/users_controller.rb'
|
42
|
+
should_generate_file 'app/helpers/users_helper.rb'
|
43
|
+
should_generate_file 'app/views/users/new.html.erb'
|
44
|
+
should_generate_file 'app/controllers/sessions_controller.rb'
|
45
|
+
should_generate_file 'app/helpers/sessions_helper.rb'
|
46
|
+
should_generate_file 'app/views/sessions/new.html.erb'
|
47
|
+
should_generate_file 'lib/authentication.rb'
|
48
|
+
should_generate_file 'test/fixtures/users.yml'
|
49
|
+
should_generate_file 'test/unit/user_test.rb'
|
50
|
+
should_generate_file 'test/functional/users_controller_test.rb'
|
51
|
+
should_generate_file 'test/functional/sessions_controller_test.rb'
|
52
|
+
|
53
|
+
should "generate migration file" do
|
54
|
+
assert !Dir.glob("#{RAILS_ROOT}/db/migrate/*.rb").empty?
|
55
|
+
end
|
56
|
+
|
57
|
+
should "generate routes" do
|
58
|
+
assert_generated_file "config/routes.rb" do |body|
|
59
|
+
assert_match "map.resources :sessions", body
|
60
|
+
assert_match "map.resources :users", body
|
61
|
+
assert_match "map.login 'login', :controller => 'sessions', :action => 'new'", body
|
62
|
+
assert_match "map.logout 'logout', :controller => 'sessions', :action => 'destroy'", body
|
63
|
+
assert_match "map.signup 'signup', :controller => 'users', :action => 'new'", body
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
should "include Authentication" do
|
68
|
+
application_controller_name = Rails.version >= '2.3.0' ? 'application_controller' : 'application'
|
69
|
+
assert_generated_file "app/controllers/#{application_controller_name}.rb" do |body|
|
70
|
+
assert_match "include Authentication", body
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
context "generator with user and session names" do
|
76
|
+
rails_generator :nifty_authentication, "Account", "CurrentSession"
|
77
|
+
should_generate_file 'app/models/account.rb'
|
78
|
+
should_generate_file 'app/controllers/accounts_controller.rb'
|
79
|
+
should_generate_file 'app/helpers/accounts_helper.rb'
|
80
|
+
should_generate_file 'app/views/accounts/new.html.erb'
|
81
|
+
should_generate_file 'app/controllers/current_sessions_controller.rb'
|
82
|
+
should_generate_file 'app/helpers/current_sessions_helper.rb'
|
83
|
+
should_generate_file 'app/views/current_sessions/new.html.erb'
|
84
|
+
should_generate_file 'test/fixtures/accounts.yml'
|
85
|
+
should_generate_file 'test/unit/account_test.rb'
|
86
|
+
should_generate_file 'test/functional/accounts_controller_test.rb'
|
87
|
+
should_generate_file 'test/functional/current_sessions_controller_test.rb'
|
88
|
+
|
89
|
+
should "generate routes" do
|
90
|
+
assert_generated_file "config/routes.rb" do |body|
|
91
|
+
assert_match "map.resources :current_sessions", body
|
92
|
+
assert_match "map.resources :accounts", body
|
93
|
+
assert_match "map.login 'login', :controller => 'current_sessions', :action => 'new'", body
|
94
|
+
assert_match "map.logout 'logout', :controller => 'current_sessions', :action => 'destroy'", body
|
95
|
+
assert_match "map.signup 'signup', :controller => 'accounts', :action => 'new'", body
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
context "generator with shoulda option" do
|
101
|
+
rails_generator :nifty_authentication, :test_framework => :shoulda
|
102
|
+
|
103
|
+
should "have controller and model tests using shoulda syntax" do
|
104
|
+
assert_generated_file "test/functional/users_controller_test.rb" do |body|
|
105
|
+
assert_match " should ", body
|
106
|
+
end
|
107
|
+
assert_generated_file "test/functional/sessions_controller_test.rb" do |body|
|
108
|
+
assert_match " should ", body
|
109
|
+
end
|
110
|
+
|
111
|
+
assert_generated_file "test/unit/user_test.rb" do |body|
|
112
|
+
assert_match " should ", body
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
context "generator with rspec option" do
|
118
|
+
rails_generator :nifty_authentication, :test_framework => :rspec
|
119
|
+
should_generate_file 'spec/fixtures/users.yml'
|
120
|
+
end
|
121
|
+
|
122
|
+
context "with spec dir" do
|
123
|
+
setup do
|
124
|
+
Dir.mkdir("#{RAILS_ROOT}/spec") unless File.exists?("#{RAILS_ROOT}/spec")
|
125
|
+
end
|
126
|
+
|
127
|
+
teardown do
|
128
|
+
FileUtils.rm_rf "#{RAILS_ROOT}/spec"
|
129
|
+
end
|
130
|
+
|
131
|
+
context "generator without arguments" do
|
132
|
+
rails_generator :nifty_authentication
|
133
|
+
should_generate_file 'spec/fixtures/users.yml'
|
134
|
+
should_generate_file 'spec/models/user_spec.rb'
|
135
|
+
should_generate_file 'spec/controllers/users_controller_spec.rb'
|
136
|
+
should_generate_file 'spec/controllers/sessions_controller_spec.rb'
|
137
|
+
end
|
138
|
+
|
139
|
+
context "generator with user and session names" do
|
140
|
+
rails_generator :nifty_authentication, "Account", "CurrentSessions"
|
141
|
+
should_generate_file 'spec/fixtures/accounts.yml'
|
142
|
+
should_generate_file 'spec/models/account_spec.rb'
|
143
|
+
should_generate_file 'spec/controllers/accounts_controller_spec.rb'
|
144
|
+
should_generate_file 'spec/controllers/current_sessions_controller_spec.rb'
|
145
|
+
end
|
146
|
+
|
147
|
+
context "generator with testunit option" do
|
148
|
+
rails_generator :nifty_authentication, :test_framework => :testunit
|
149
|
+
should_generate_file 'test/fixtures/users.yml'
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
context "generator with haml option" do
|
154
|
+
rails_generator :nifty_authentication, :haml => true
|
155
|
+
|
156
|
+
should_generate_file "app/views/users/new.html.haml"
|
157
|
+
should_generate_file "app/views/sessions/new.html.haml"
|
158
|
+
end
|
159
|
+
|
160
|
+
context "generator with authlogic option and custom account name" do
|
161
|
+
rails_generator :nifty_authentication, "Account", :authlogic => true
|
162
|
+
should_generate_file 'app/models/account.rb'
|
163
|
+
should_generate_file 'app/controllers/accounts_controller.rb'
|
164
|
+
should_generate_file 'app/helpers/accounts_helper.rb'
|
165
|
+
should_generate_file 'app/views/accounts/new.html.erb'
|
166
|
+
should_generate_file 'app/controllers/account_sessions_controller.rb'
|
167
|
+
should_generate_file 'app/helpers/account_sessions_helper.rb'
|
168
|
+
should_generate_file 'app/views/account_sessions/new.html.erb'
|
169
|
+
should_generate_file 'test/fixtures/accounts.yml'
|
170
|
+
should_generate_file 'test/unit/account_test.rb'
|
171
|
+
should_generate_file 'test/functional/accounts_controller_test.rb'
|
172
|
+
should_generate_file 'test/functional/account_sessions_controller_test.rb'
|
173
|
+
should_generate_file 'lib/authentication.rb'
|
174
|
+
|
175
|
+
should "only include acts_as_authentic in account model" do
|
176
|
+
assert_generated_file "app/models/account.rb" do |body|
|
177
|
+
assert_match "acts_as_authentic", body
|
178
|
+
assert_no_match(/validates/, body)
|
179
|
+
assert_no_match(/def/, body)
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
should "should generate authentication module with current_account_session method" do
|
184
|
+
assert_generated_file "lib/authentication.rb" do |body|
|
185
|
+
assert_match "def current_account_session", body
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
should "should generate AccountSession model" do
|
190
|
+
assert_generated_file "app/models/account_session.rb" do |body|
|
191
|
+
assert_match "class AccountSession < Authlogic::Session::Base", body
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
195
|
+
should "should generate restful style actions in sessions controller" do
|
196
|
+
assert_generated_file "app/controllers/account_sessions_controller.rb" do |body|
|
197
|
+
assert_match "AccountSession.new", body
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
should "should generate form for account session" do
|
202
|
+
assert_generated_file "app/views/account_sessions/new.html.erb" do |body|
|
203
|
+
assert_match "form_for @account_session", body
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
should "should not include tests in account since authlogic is already tested" do
|
208
|
+
assert_generated_file "test/unit/account_test.rb" do |body|
|
209
|
+
assert_no_match(/def test/, body)
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
213
|
+
should "should handle session controller tests differently" do
|
214
|
+
assert_generated_file "test/functional/account_sessions_controller_test.rb" do |body|
|
215
|
+
assert_match "AccountSession.find", body
|
216
|
+
end
|
217
|
+
end
|
218
|
+
|
219
|
+
should "generate migration with authlogic columns" do
|
220
|
+
file = Dir.glob("#{RAILS_ROOT}/db/migrate/*.rb").first
|
221
|
+
assert file, "migration file doesn't exist"
|
222
|
+
assert_match(/[0-9]+_create_accounts.rb$/, file)
|
223
|
+
assert_generated_file "db/migrate/#{File.basename(file)}" do |body|
|
224
|
+
assert_match "class CreateAccounts", body
|
225
|
+
assert_match "t.string :username", body
|
226
|
+
assert_match "t.string :email", body
|
227
|
+
assert_match "t.string :crypted_password", body
|
228
|
+
assert_match "t.string :password_salt", body
|
229
|
+
assert_match "t.string :persistence_token", body
|
230
|
+
assert_match "t.timestamps", body
|
231
|
+
assert_no_match(/password_hash/, body)
|
232
|
+
end
|
233
|
+
end
|
234
|
+
end
|
235
|
+
|
236
|
+
context "generator with authlogic option and custom account and session name" do
|
237
|
+
rails_generator :nifty_authentication, "Account", "UserSession", :authlogic => true
|
238
|
+
should_generate_file 'app/controllers/user_sessions_controller.rb'
|
239
|
+
should_generate_file 'app/helpers/user_sessions_helper.rb'
|
240
|
+
should_generate_file 'app/views/user_sessions/new.html.erb'
|
241
|
+
should_generate_file 'test/functional/user_sessions_controller_test.rb'
|
242
|
+
|
243
|
+
should "should generate authentication module with current_user_session method" do
|
244
|
+
assert_generated_file "lib/authentication.rb" do |body|
|
245
|
+
assert_match "def current_user_session", body
|
246
|
+
end
|
247
|
+
end
|
248
|
+
|
249
|
+
should "should generate UserSession model" do
|
250
|
+
assert_generated_file "app/models/user_session.rb" do |body|
|
251
|
+
assert_match "class UserSession < Authlogic::Session::Base", body
|
252
|
+
end
|
253
|
+
end
|
254
|
+
|
255
|
+
should "should generate restful style actions in sessions controller" do
|
256
|
+
assert_generated_file "app/controllers/user_sessions_controller.rb" do |body|
|
257
|
+
assert_match "UserSession.new", body
|
258
|
+
end
|
259
|
+
end
|
260
|
+
|
261
|
+
should "should generate form for user session" do
|
262
|
+
assert_generated_file "app/views/user_sessions/new.html.erb" do |body|
|
263
|
+
assert_match "form_for @user_session", body
|
264
|
+
end
|
265
|
+
end
|
266
|
+
|
267
|
+
should "should handle session controller tests differently" do
|
268
|
+
assert_generated_file "test/functional/user_sessions_controller_test.rb" do |body|
|
269
|
+
assert_match "UserSession.find", body
|
270
|
+
end
|
271
|
+
end
|
272
|
+
end
|
273
|
+
end
|
274
|
+
end
|