Browse code

Login cez xmpp4r

Boli pridane potrebne gemy pre testovanie. Chybove hlasky su ulozene v prekladovom subore.

Cinan Rakosnik authored on 11/02/2013 at 22:49:08
Showing 14 changed files
... ...
@@ -5,4 +5,4 @@ You are allowed to:
5 5
 2. Remove generators
6 6
 3. Add installed generators
7 7
 To add new installed generators automatically delete this file and reload the project.
8
+--><GeneratorsGroup><Generator name="active_record:migration" /><Generator name="active_record:model" /><Generator name="active_record:observer" /><Generator name="active_record:session_migration" /><Generator name="assets" /><Generator name="coffee:assets" /><Generator name="controller" /><Generator name="erb:controller" /><Generator name="erb:mailer" /><Generator name="erb:scaffold" /><Generator name="generator" /><Generator name="helper" /><Generator name="integration_test" /><Generator name="jquery:install" /><Generator name="js:assets" /><Generator name="mailer" /><Generator name="migration" /><Generator name="model" /><Generator name="mongoid:config" /><Generator name="observer" /><Generator name="performance_test" /><Generator name="resource" /><Generator name="rspec:controller" /><Generator name="rspec:helper" /><Generator name="rspec:install" /><Generator name="rspec:integration" /><Generator name="rspec:mailer" /><Generator name="rspec:model" /><Generator name="rspec:observer" /><Generator name="rspec:scaffold" /><Generator name="rspec:view" /><Generator name="scaffold" /><Generator name="scaffold_controller" /><Generator name="session_migration" /><Generator name="task" /></GeneratorsGroup></Settings>
... ...
@@ -32,6 +32,10 @@ group :test do
32 32
 	gem 'minitest-reporters', '>= 0.5.0'
33 33
 	gem 'test-unit'
34 34
 	gem 'timecop'
35
+	gem "rspec-rails"
36
+	gem "capybara"
37
+	gem "launchy"
38
+	gem "factory_girl_rails"
35 39
 end
36 40
 
37 41
 gem 'jquery-rails'
... ...
@@ -28,6 +28,7 @@ GEM
28 28
     activesupport (3.2.11)
29 29
       i18n (~> 0.6)
30 30
       multi_json (~> 1.0)
31
+    addressable (2.3.2)
31 32
     ansi (1.4.3)
32 33
     arel (3.0.2)
33 34
     builder (3.0.4)
... ...
@@ -37,6 +38,15 @@ GEM
37 37
       net-sftp (>= 2.0.0)
38 38
       net-ssh (>= 2.0.14)
39 39
       net-ssh-gateway (>= 1.1.0)
40
+    capybara (2.0.2)
41
+      mime-types (>= 1.16)
42
+      nokogiri (>= 1.3.3)
43
+      rack (>= 1.0.0)
44
+      rack-test (>= 0.5.4)
45
+      selenium-webdriver (~> 2.0)
46
+      xpath (~> 1.0.0)
47
+    childprocess (0.3.7)
48
+      ffi (~> 1.0, >= 1.0.6)
40 49
     chunky_png (1.2.7)
41 50
     coffee-rails (3.2.2)
42 51
       coffee-script (>= 2.2.0)
... ...
@@ -49,9 +59,16 @@ GEM
49 49
       chunky_png (~> 1.2)
50 50
       fssm (>= 0.2.7)
51 51
       sass (~> 3.1)
52
+    diff-lcs (1.1.3)
52 53
     erubis (2.7.0)
53 54
     execjs (1.4.0)
54 55
       multi_json (~> 1.0)
56
+    factory_girl (4.2.0)
57
+      activesupport (>= 3.0.0)
58
+    factory_girl_rails (4.2.0)
59
+      factory_girl (~> 4.2.0)
60
+      railties (>= 3.0.0)
61
+    ffi (1.3.1)
55 62
     fssm (0.2.10)
56 63
     haml (3.1.7)
57 64
     haml-rails (0.3.5)
... ...
@@ -68,6 +85,8 @@ GEM
68 68
       railties (>= 3.0, < 5.0)
69 69
       thor (>= 0.14, < 2.0)
70 70
     json (1.7.6)
71
+    launchy (2.2.0)
72
+      addressable (~> 2.3)
71 73
     mail (2.4.4)
72 74
       i18n (>= 0.4.0)
73 75
       mime-types (~> 1.16)
... ...
@@ -93,6 +112,7 @@ GEM
93 93
     net-ssh (2.6.3)
94 94
     net-ssh-gateway (1.1.0)
95 95
       net-ssh (>= 1.99.1)
96
+    nokogiri (1.5.6)
96 97
     origin (1.0.11)
97 98
     polyglot (0.3.3)
98 99
     powerbar (1.0.11)
... ...
@@ -123,11 +143,28 @@ GEM
123 123
     rake (10.0.3)
124 124
     rdoc (3.12)
125 125
       json (~> 1.4)
126
+    rspec-core (2.12.2)
127
+    rspec-expectations (2.12.1)
128
+      diff-lcs (~> 1.1.3)
129
+    rspec-mocks (2.12.2)
130
+    rspec-rails (2.12.2)
131
+      actionpack (>= 3.0)
132
+      activesupport (>= 3.0)
133
+      railties (>= 3.0)
134
+      rspec-core (~> 2.12.0)
135
+      rspec-expectations (~> 2.12.0)
136
+      rspec-mocks (~> 2.12.0)
137
+    rubyzip (0.9.9)
126 138
     sass (3.2.5)
127 139
     sass-rails (3.2.6)
128 140
       railties (~> 3.2.0)
129 141
       sass (>= 3.1.10)
130 142
       tilt (~> 1.3)
143
+    selenium-webdriver (2.29.0)
144
+      childprocess (>= 0.2.5)
145
+      multi_json (~> 1.0)
146
+      rubyzip
147
+      websocket (~> 1.0.4)
131 148
     sprockets (2.2.2)
132 149
       hike (~> 1.2)
133 150
       multi_json (~> 1.0)
... ...
@@ -144,22 +181,29 @@ GEM
144 144
     uglifier (1.3.0)
145 145
       execjs (>= 0.3.0)
146 146
       multi_json (~> 1.0, >= 1.0.2)
147
+    websocket (1.0.7)
147 148
     xmpp4r (0.5)
149
+    xpath (1.0.0)
150
+      nokogiri (~> 1.3)
148 151
 
149 152
 PLATFORMS
150 153
   ruby
151 154
 
152 155
 DEPENDENCIES
153 156
   capistrano
157
+  capybara
154 158
   coffee-rails (~> 3.2.1)
155 159
   compass (~> 0.12.2)
160
+  factory_girl_rails
156 161
   haml (~> 3.1.7)
157 162
   haml-rails (~> 0.3.5)
158 163
   jquery-rails
164
+  launchy
159 165
   minitest
160 166
   minitest-reporters (>= 0.5.0)
161 167
   mongoid (~> 3.0.20)
162 168
   rails (= 3.2.11)
169
+  rspec-rails
163 170
   sass-rails (~> 3.2.3)
164 171
   test-unit
165 172
   timecop
... ...
@@ -4,7 +4,17 @@ class SessionsController < ApplicationController
4 4
 	end
5 5
 
6 6
 	def create
7
+		require_dependency "signin.rb"
7 8
 
9
+		begin
10
+			Signin.try_login(params[:jid].downcase, params[:password])
11
+		rescue Signin::LoginError
12
+			flash.now[:error] = I18n.t 'login.error'
13
+			return render 'new'
14
+		end
15
+
16
+		flash.now[:notice] = I18n.t 'login.success'
17
+		render 'new'
8 18
 	end
9 19
 
10 20
 	def destroy
11 21
new file mode 100644
... ...
@@ -0,0 +1,29 @@
0
+require 'json/pure'
1
+require 'xmpp4r'
2
+require 'xmpp4r/roster'
3
+require 'cgi'
4
+
5
+module Signin
6
+	include Jabber
7
+
8
+	def self.try_login(jid, pass)
9
+		@client = Client.new(jid)
10
+
11
+		Jabber::debug = (Rails.env == 'development')
12
+
13
+		begin
14
+			connect(pass)
15
+		rescue
16
+			raise LoginError
17
+		end
18
+	end
19
+
20
+	def self.connect(pass)
21
+		@client.connect()
22
+		@client.auth(pass)
23
+	end
24
+
25
+	class LoginError < Exception
26
+
27
+	end
28
+end
0 29
\ No newline at end of file
1 30
new file mode 100644
... ...
@@ -0,0 +1,5 @@
0
+class User
1
+	include Mongoid::Document
2
+
3
+	field :jid, type: String
4
+end
0 5
\ No newline at end of file
1 6
deleted file mode 100644
... ...
@@ -1,5 +0,0 @@
1
-class Users
2
-	include Mongoid::Document
3
-
4
-	field :jid, type: String
5
-end
6 1
\ No newline at end of file
... ...
@@ -4,6 +4,5 @@
4 4
     %title= yield(:title) + " | App"
5 5
     %meta{:charset => "utf-8"}/
6 6
     -#%link{ :href => "/css/screen.css", :media => "screen", :rel => "stylesheet" }
7
-    = csrf_meta_tags
8 7
   %body
9 8
     = yield
10 9
\ No newline at end of file
... ...
@@ -1,3 +1,20 @@
1 1
 - content_for(:title, 'Login')
2
+%h1 Login
3
+- if flash[:error]
4
+  %div.alert.alert-error
5
+    = flash[:error]
2 6
 
3
-Hello
4 7
\ No newline at end of file
8
+- if flash[:notice]
9
+  %div.alert.alert-notice
10
+    = flash[:notice]
11
+
12
+%form{ action: sessions_path, method: "post"}
13
+  %fieldset
14
+    %ol
15
+      %li
16
+        %label{for: "jid"} Jabber ID:
17
+        %input{type: "text", name: "jid", class: "text", id: "jid"}
18
+      %li
19
+        %label{for: "password"} Password:
20
+        %input{type: "password", name: "password", class: "text", id: "password"}
21
+    %input{type: "submit", value: "Send", class: "button"}
5 22
\ No newline at end of file
6 23
new file mode 100644
... ...
@@ -2,4 +2,6 @@
2 2
 # See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
3 3
 
4 4
 en:
5
-  hello: "Hello world"
5
+  login:
6
+    error: 'Invalid email/password combination'
7
+    success: 'Successfully logged in'
6 8
\ No newline at end of file
7 9
new file mode 100644
... ...
@@ -0,0 +1,6 @@
0
+FactoryGirl.define do
1
+	factory :user do
2
+		jid      "cinan6@jabbim.cz"
3
+		password "pass"
4
+	end
5
+end
0 6
\ No newline at end of file
1 7
new file mode 100644
... ...
@@ -0,0 +1,39 @@
0
+require 'spec_helper'
1
+
2
+describe "Authentication" do
3
+
4
+	subject { page }
5
+
6
+	describe "signin page" do
7
+		before { visit signin_path }
8
+
9
+		it { should have_selector('h1',    text: 'Login') }
10
+		it { should have_title('Login') }
11
+	end
12
+
13
+	describe "signin" do
14
+		before { visit signin_path }
15
+
16
+		describe "with invalid information" do
17
+			before { click_button "Send" }
18
+
19
+			it { should have_title('Login') }
20
+			it { should have_selector('div.alert.alert-error', text: 'Invalid') }
21
+		end
22
+
23
+		describe "with valid information" do
24
+			before do
25
+				fill_in "jid",      with: 'bakalar@jabbim.cz'
26
+				fill_in "password", with: 'tralala'
27
+				click_button "Send"
28
+			end
29
+
30
+			#it { should have_title(user.name) }
31
+			#it { should have_link('Profile', href: user_path(user)) }
32
+			#it { should have_link('Logout', href: signout_path) }
33
+			#it { should_not have_link('Login', href: signin_path) }
34
+
35
+			it {should have_selector('div.alert.alert-notice', text: 'Success')}
36
+		end
37
+	end
38
+end
0 39
\ No newline at end of file
1 40
new file mode 100644
... ...
@@ -0,0 +1,44 @@
0
+# This file is copied to spec/ when you run 'rails generate rspec:install'
1
+ENV["RAILS_ENV"] ||= 'test'
2
+require File.expand_path("../../config/environment", __FILE__)
3
+require 'rspec/rails'
4
+require 'rspec/autorun'
5
+
6
+RSpec::Matchers::define :have_title do |text|
7
+	match do |page|
8
+		Capybara.string(page.body).has_selector?('title', text: text)
9
+	end
10
+end
11
+
12
+# Requires supporting ruby files with custom matchers and macros, etc,
13
+# in spec/support/ and its subdirectories.
14
+Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
15
+
16
+RSpec.configure do |config|
17
+  # ## Mock Framework
18
+  #
19
+  # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
20
+  #
21
+  # config.mock_with :mocha
22
+  # config.mock_with :flexmock
23
+  # config.mock_with :rr
24
+
25
+  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
26
+  #config.fixture_path = "#{::Rails.root}/spec/fixtures"
27
+
28
+  # If you're not using ActiveRecord, or you'd prefer not to run each of your
29
+  # examples within a transaction, remove the following line or assign false
30
+  # instead of true.
31
+  #config.use_transactional_fixtures = true
32
+
33
+  # If true, the base class of anonymous controllers will be inferred
34
+  # automatically. This will be the default behavior in future versions of
35
+  # rspec-rails.
36
+  config.infer_base_class_for_anonymous_controllers = false
37
+
38
+  # Run specs in random order to surface order dependencies. If you find an
39
+  # order dependency and want to debug it, you can fix the order by providing
40
+  # the seed, which is printed after each run.
41
+  #     --seed 1234
42
+  config.order = "random"
43
+end