inboxes 0.2.0 → 0.2.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.
@@ -0,0 +1,97 @@
1
+ require 'active_record'
2
+ require 'action_controller/railtie'
3
+ require 'action_view/railtie'
4
+
5
+ require "cancan"
6
+ require "cancan/ability"
7
+ require "cancan/controller_resource"
8
+ require "cancan/controller_additions"
9
+
10
+ require 'devise'
11
+ require 'devise/orm/active_record'
12
+
13
+ # database
14
+ ActiveRecord::Base.configurations = {'test' => {:adapter => 'sqlite3', :database => ':memory:'}}
15
+ ActiveRecord::Base.establish_connection('test')
16
+
17
+ # config
18
+ app = Class.new(Rails::Application)
19
+ app.config.secret_token = "3b7cd727ee24e8444053437c36cc66c4"
20
+ app.config.session_store :cookie_store, :key => "_myapp_session"
21
+ app.config.active_support.deprecation = :log
22
+ app.initialize!
23
+
24
+ require 'devise_config'
25
+
26
+ # models
27
+ class User < ActiveRecord::Base
28
+ devise :database_authenticatable, :registerable,
29
+ :recoverable, :rememberable, :trackable, :validatable
30
+ attr_accessible :email, :password, :password_confirmation, :remember_me, :name
31
+ validates :name, :presence => true, :uniqueness => true
32
+ has_inboxes
33
+ end
34
+
35
+ # routes
36
+ app.routes.draw do
37
+ devise_for :users
38
+ end
39
+
40
+ #migrations
41
+ ActiveRecord::Base.silence do
42
+ ActiveRecord::Migration.verbose = false
43
+ ActiveRecord::Schema.define :version => 0 do
44
+ create_table "users", :force => true do |t|
45
+ t.string "email", :default => "", :null => false
46
+ t.string "encrypted_password", :limit => 128, :default => "", :null => false
47
+ t.string "reset_password_token"
48
+ t.datetime "reset_password_sent_at"
49
+ t.datetime "remember_created_at"
50
+ t.integer "sign_in_count", :default => 0
51
+ t.datetime "current_sign_in_at"
52
+ t.datetime "last_sign_in_at"
53
+ t.string "current_sign_in_ip"
54
+ t.string "last_sign_in_ip"
55
+ t.datetime "created_at"
56
+ t.datetime "updated_at"
57
+ t.string "name"
58
+ end
59
+
60
+ create_table "discussions", :force => true do |t|
61
+ t.integer "user_id"
62
+ t.integer "messages_count", :default => 0
63
+ t.boolean "is_private", :default => true
64
+ t.datetime "created_at"
65
+ t.datetime "updated_at"
66
+ end
67
+
68
+ create_table "messages", :force => true do |t|
69
+ t.integer "user_id"
70
+ t.integer "discussion_id"
71
+ t.text "body"
72
+ t.datetime "created_at"
73
+ t.datetime "updated_at"
74
+ end
75
+
76
+ create_table "speakers", :force => true do |t|
77
+ t.integer "user_id"
78
+ t.integer "discussion_id"
79
+ t.datetime "created_at"
80
+ t.datetime "updated_at"
81
+ end
82
+ end
83
+ end
84
+
85
+ # controllers
86
+ class ApplicationController < ActionController::Base
87
+ before_filter :assign_unread_discussions
88
+
89
+ private
90
+
91
+ def assign_unread_discussions
92
+ @unread_discussions_count = Discussion.unread_for(current_user).count if user_signed_in?
93
+ end
94
+ end
95
+
96
+ # helpers
97
+ Object.const_set(:ApplicationHelper, Module.new)
@@ -0,0 +1,6 @@
1
+ # Simulate a gem providing a subclass of ActiveRecord::Base before the Railtie is loaded.
2
+
3
+ require 'active_record'
4
+
5
+ class GemDefinedModel < ActiveRecord::Base
6
+ end
@@ -0,0 +1,104 @@
1
+ require 'spec_helper'
2
+
3
+ describe Discussion do
4
+
5
+ it "should create valid group discussion" do
6
+ discussion = Discussion.new
7
+ discussion.recipient_ids = [Factory(:user), Factory(:user), Factory(:user)].map { |u| u.id }
8
+ discussion.save.should be true
9
+
10
+ discussion.users.count.should be == 3
11
+ # discussion.private?.should be false
12
+ end
13
+
14
+ it "should create valid private discussion" do
15
+ discussion = Discussion.new
16
+ discussion.recipient_ids = [Factory(:user).id, Factory(:user).id]
17
+ discussion.save.should be true
18
+ discussion.private?.should be true
19
+ end
20
+
21
+ it "should add messages to valid discussion" do
22
+ discussion = Discussion.new
23
+ discussion.recipient_ids = [Factory(:user), Factory(:user)].map { |u| u.id }
24
+ discussion.save.should be true
25
+
26
+ message = Message.new(:user => Factory(:user), :body => Factory.next(:string), :discussion => discussion)
27
+ message.save.should be true
28
+ end
29
+
30
+ it "should not create discussion without repicients" do
31
+ discussion = Discussion.new
32
+ discussion.save.should be false
33
+ end
34
+
35
+ it "should not add speakers if discussion is not saved" do
36
+ discussion = Discussion.new
37
+ lambda { discussion.add_speaker(Factory(:user)) }.should raise_error(ArgumentError)
38
+ end
39
+
40
+ it "should add speakers to discussion" do
41
+ discussion = Factory(:discussion)
42
+ user = Factory(:user)
43
+ discussion.add_speaker(user)
44
+ discussion.users.should include user
45
+ end
46
+
47
+ it "should remove speaker from discussion" do
48
+ discussion = Factory(:discussion)
49
+ user = Factory(:user)
50
+ discussion.add_speaker(user)
51
+ discussion.remove_speaker(user)
52
+
53
+ discussion.users.should_not include user
54
+ end
55
+
56
+ it "should assign discussion as viewed for user" do
57
+ user = Factory(:user)
58
+ discussion = Factory(:discussion)
59
+ discussion.mark_as_read_for(user)
60
+ discussion.unread_for?(user).should be false
61
+ end
62
+
63
+ it "model with new message should be unread for user" do
64
+ discussion = Factory(:discussion)
65
+ message = Message.create!(:user => Factory(:user), :body => Factory.next(:string), :discussion => discussion)
66
+ discussion.unread_for?(Factory(:user)).should be true
67
+ end
68
+
69
+ it "attribute user_invited_at should be right" do
70
+ discussion = Factory(:discussion)
71
+ user = Factory(:user)
72
+ speaker = discussion.add_speaker(user)
73
+ speaker.created_at.to_i.should be == discussion.user_invited_at(user).to_i
74
+ end
75
+
76
+ it "method exists_between_users? should be right" do
77
+ user = Factory(:user)
78
+ user2 = Factory(:user)
79
+ discussion = Discussion.create!(:recipient_ids => [user.id, user2.id])
80
+
81
+ Discussion.find_between_users(user, user2).should be == discussion
82
+ Discussion.find_between_users(user2, user).should be == discussion
83
+ end
84
+
85
+ it "method exists_between_users? should be right if there is no discussion" do
86
+ user = Factory(:user)
87
+ user2 = Factory(:user)
88
+
89
+ Discussion.find_between_users(user, user2).should be_nil
90
+ Discussion.find_between_users(user2, user).should be_nil
91
+ end
92
+
93
+ it "method exists_between_users? should be right if 2 users are in group discussion" do
94
+ user = Factory(:user)
95
+ user2 = Factory(:user)
96
+ user3 = Factory(:user)
97
+ discussion = Discussion.create!(:recipient_ids => [user.id, user2.id, user3.id])
98
+
99
+ Discussion.find_between_users(user, user2).should be_nil
100
+ Discussion.find_between_users(user2, user).should be_nil
101
+ Discussion.find_between_users(user2, user3).should be_nil
102
+ end
103
+
104
+ end
@@ -0,0 +1,96 @@
1
+ require 'spec_helper'
2
+
3
+ describe Inboxes::DiscussionsController do
4
+ context "Guest" do
5
+ it "should not see discussions list" do
6
+ get :index
7
+ response.should redirect_to(sign_in_url)
8
+ end
9
+
10
+ it "should not see new action" do
11
+ get :new
12
+ response.should redirect_to(sign_in_url)
13
+ end
14
+
15
+ it "should not create discussion if model is valid" do
16
+ recipient_ids = [Factory(:user).id, Factory(:user).id]
17
+ post(:create,
18
+ :discussion => {
19
+ :recipient_ids => recipient_ids,
20
+ :messages_attributes => {
21
+ 0 => {:body => Factory.next(:string)}
22
+ }
23
+ }
24
+ )
25
+
26
+ response.should redirect_to(sign_in_url)
27
+ end
28
+
29
+ end
30
+
31
+ context("Authenticated admin") do
32
+ before(:each) do
33
+ @request.env["devise.mapping"] = Devise.mappings[:user]
34
+ @user = Factory(:user)
35
+ @user.set_role(:admin)
36
+ sign_in @user
37
+ end
38
+
39
+ it "should see discussions list" do
40
+ get :index
41
+ response.should render_template(:index)
42
+ end
43
+
44
+ it "should see new action" do
45
+ get :new
46
+ response.should render_template(:new)
47
+ end
48
+
49
+ it "should open discussion" do
50
+ discussion = Factory.build(:discussion)
51
+ discussion.recipient_ids = [@user, Factory(:user)].map { |u| u.id }
52
+ discussion.save.should be true
53
+
54
+ get(:show, :id => discussion)
55
+ response.status.should be 200
56
+ end
57
+
58
+ it "should create private discussion if model is valid" do
59
+ recipient_ids = [Factory(:user).id, Factory(:user).id]
60
+ post(:create,
61
+ :discussion => {
62
+ :recipient_ids => recipient_ids,
63
+ :messages_attributes => {
64
+ 0 => {:body => Factory.next(:string)}
65
+ }
66
+ }
67
+ )
68
+
69
+ response.should redirect_to(discussion_url(assigns[:discussion]))
70
+ end
71
+
72
+ it "should create group discussion if model is valid" do
73
+ recipient_ids = [Factory(:user).id, Factory(:user).id, Factory(:user).id]
74
+ post(:create,
75
+ :discussion => {
76
+ :recipient_ids => recipient_ids,
77
+ :messages_attributes => {
78
+ 0 => {:body => Factory.next(:string)}
79
+ }
80
+ }
81
+ )
82
+
83
+ response.should redirect_to(discussion_url(assigns[:discussion]))
84
+ end
85
+
86
+ it "should not create discussion with empty message" do
87
+ discussion = Discussion.new
88
+ discussion.recipient_ids = [Factory(:user).id, Factory(:user).id, Factory(:user).id]
89
+ post(:create, :discussion => discussion)
90
+
91
+ response.should render_template(:new)
92
+ end
93
+
94
+ end
95
+
96
+ end
@@ -0,0 +1,23 @@
1
+ require "spec_helper"
2
+
3
+ describe Inboxes::DiscussionsController do
4
+ describe "routing" do
5
+
6
+ it "routes to #index" do
7
+ get("/discussions").should route_to("inboxes/discussions#index")
8
+ end
9
+
10
+ it "routes to #new" do
11
+ get("/discussions/new").should route_to("inboxes/discussions#new")
12
+ end
13
+
14
+ it "routes to #show" do
15
+ get("/discussions/1").should route_to("inboxes/discussions#show", :id => "1")
16
+ end
17
+
18
+ it "routes to #create" do
19
+ post("/discussions").should route_to("inboxes/discussions#create")
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ describe Message do
4
+ it "should not be visible to new speaker" do
5
+ discussion = Factory(:discussion)
6
+ old_user = Factory(:user)
7
+ discussion.add_speaker(old_user)
8
+ message = Message.create!(:discussion => discussion, :user => old_user, :body => Factory.next(:string))
9
+
10
+ sleep 2
11
+
12
+ new_user = Factory(:user)
13
+ discussion.add_speaker(new_user)
14
+ message.visible_for?(new_user).should be_false
15
+ end
16
+
17
+ it "should be visible to old speaker" do
18
+ discussion = Factory(:discussion)
19
+ user = Factory(:user)
20
+ discussion.add_speaker(user)
21
+ message = Message.create!(:discussion => discussion, :user => user, :body => Factory.next(:string))
22
+
23
+ sleep 5
24
+
25
+ message.visible_for?(user).should be_true
26
+ end
27
+ end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ describe Inboxes::MessagesController do
4
+
5
+ render_views
6
+
7
+ context "Guest" do
8
+ it "should redirect guest if he wants to create message" do
9
+ discussion = Factory(:discussion)
10
+ # puts discussion.id
11
+ post :create, :discussion_id => discussion.id
12
+ response.should redirect_to(sign_in_url)
13
+ end
14
+ end
15
+
16
+ context "Authenticated admin" do
17
+ before(:each) do
18
+ @request.env["devise.mapping"] = Devise.mappings[:user]
19
+ @admin = Factory(:admin)
20
+ @admin.set_role(:admin)
21
+ sign_in @admin
22
+ end
23
+
24
+ it "create action should redirect to discussion when model is valid" do
25
+ Message.any_instance.stubs(:valid?).returns(true)
26
+ message = Factory(:message)
27
+ user = Factory(:user)
28
+ discussion = Factory(:discussion, :recipient_ids => [@admin.id, user.id])
29
+ post(:create, :discussion_id => discussion.id)
30
+ response.should redirect_to(discussion_url(discussion))
31
+ end
32
+
33
+ # it "create action should assign flash with error message" do
34
+ # Comment.any_instance.stubs(:valid?).returns(false)
35
+ # first_post = Factory(:post)
36
+ # post :create, :post_id => first_post
37
+ #
38
+ # flash[:notice].should =~ /Введите текст комментария!/i
39
+ # end
40
+ end
41
+
42
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe Speaker do
4
+ # it "should create valid model" do
5
+ # speaker = Speaker.new
6
+ # speaker.user = Factory(:user)
7
+ # speaker.discussion = Factory(:discussion)
8
+ # speaker.save.should be true
9
+ # end
10
+ #
11
+ # it "should bot create model without discussion" do
12
+ # speaker = Speaker.new
13
+ # speaker.user = Factory(:user)
14
+ # speaker.save.should be false
15
+ # end
16
+ end
@@ -0,0 +1,72 @@
1
+ require 'spec_helper'
2
+
3
+ describe Inboxes::SpeakersController do
4
+
5
+ context("Authenticated admin") do
6
+ before(:each) do
7
+ @request.env["devise.mapping"] = Devise.mappings[:user]
8
+ @user = Factory(:user)
9
+ @user.set_role(:admin)
10
+ sign_in @user
11
+ end
12
+
13
+ it "should add speaker to discussion" do
14
+ discussion = Factory(:discussion, :recipient_ids => [@user.id, Factory(:user).id])
15
+ post(:create,
16
+ :discussion_id => discussion,
17
+ :speaker => {
18
+ :user_id => Factory(:user).id
19
+ }
20
+ )
21
+ response.should redirect_to(discussion_url(discussion))
22
+ flash[:notice].should =~ /Собеседник успешно добавлен/i
23
+ end
24
+
25
+ # it "should not add bad speaker to discussion" do
26
+ # discussion = Factory(:discussion)
27
+ #
28
+ # lambda {
29
+ # post(:create,
30
+ # :discussion_id => discussion
31
+ # )
32
+ # }.should raise_error(ActiveRecord::RecordNotFound)
33
+ # end
34
+ end
35
+
36
+ context("User") do
37
+ before(:each) do
38
+ @request.env["devise.mapping"] = Devise.mappings[:user]
39
+ @user = Factory(:user)
40
+ @user.set_role(:user)
41
+ sign_in @user
42
+ end
43
+
44
+ it "should add speaker to discussion if he is participant if this discussion" do
45
+ Speaker.any_instance.stubs(:valid?).returns(true)
46
+ discussion = Factory(:discussion, :recipient_ids => [@user.id, Factory(:user).id])
47
+ # puts discussion.can_participate?(@user)
48
+ # new_user = Factory(:user)
49
+ post(:create,
50
+ :discussion_id => discussion,
51
+ :speaker => {
52
+ :user_id => Factory(:user).id
53
+ }
54
+ )
55
+ response.should redirect_to(discussion_url(discussion))
56
+ # response.should redirect_to(root_url)
57
+ flash[:notice].should =~ /Собеседник успешно добавлен/i
58
+ end
59
+
60
+ # дописать спек
61
+ it "should not add speaker to discussion if he is not participant if this discussion" do
62
+ discussion = Factory(:discussion)
63
+ post(:create,
64
+ :discussion_id => discussion,
65
+ :speaker => {
66
+ :user_id => Factory(:user).id
67
+ }
68
+ )
69
+ response.should redirect_to(root_url)
70
+ end
71
+ end
72
+ end