omroep_auth 0.0.0 → 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.0
1
+ 0.0.1
@@ -0,0 +1,4 @@
1
+ module OmroepAuth
2
+ # nothing to see here
3
+ # the magic happens in rails_generators/
4
+ end
@@ -0,0 +1,61 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{omroep_auth}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Bart Zonneveld"]
12
+ s.date = %q{2010-01-26}
13
+ s.description = %q{longer description of your gem}
14
+ s.email = %q{loop@superinfinite.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/omroep_auth.rb",
27
+ "omroep_auth.gemspec",
28
+ "rails_generators/omroep_auth_generator.rb",
29
+ "rails_generators/templates/config/initializers/omroep_auth.rb",
30
+ "rails_generators/templates/controllers/omroep_auth_controller.rb",
31
+ "rails_generators/templates/lib/omroep_auth_system.rb",
32
+ "rails_generators/templates/migrate/create_omroep_users.rb",
33
+ "rails_generators/templates/models/omroep_user.rb",
34
+ "rails_generators/templates/spec/controllers/omroep_auth_controller_spec.rb",
35
+ "rails_generators/templates/spec/controllers/omroep_auth_system_spec.rb",
36
+ "rails_generators/templates/spec/models/omroep_user_spec.rb",
37
+ "rails_generators/templates/views/omroep_auth/new.html.erb",
38
+ "test/helper.rb",
39
+ "test/test_omroep_auth.rb"
40
+ ]
41
+ s.homepage = %q{http://github.com/bartzon/omroep_auth}
42
+ s.rdoc_options = ["--charset=UTF-8"]
43
+ s.require_paths = ["lib"]
44
+ s.rubygems_version = %q{1.3.5}
45
+ s.summary = %q{one-line summary of your gem}
46
+ s.test_files = [
47
+ "test/helper.rb",
48
+ "test/test_omroep_auth.rb"
49
+ ]
50
+
51
+ if s.respond_to? :specification_version then
52
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
53
+ s.specification_version = 3
54
+
55
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
56
+ else
57
+ end
58
+ else
59
+ end
60
+ end
61
+
@@ -0,0 +1,32 @@
1
+ class OmroepAuthGenerator < Rails::Generator::Base
2
+ def manifest
3
+ record do |m|
4
+ m.directory "app/controllers"
5
+ m.file "controllers/omroep_auth_controller.rb", "app/controllers/omroep_auth_controller.rb"
6
+
7
+ m.directory "app/models"
8
+ m.file "models/omroep_user.rb", "app/models/omroep_user.rb"
9
+
10
+ m.directory "app/views/omroep_auth"
11
+ m.file "views/omroep_auth/new.html.erb", "app/views/omroep_auth/new.html.erb"
12
+
13
+ m.directory "spec/controllers"
14
+ m.file "spec/controllers/omroep_auth_controller_spec.rb", "spec/controllers/omroep_auth_controller_spec.rb"
15
+ m.file "spec/controllers/omroep_auth_system_spec.rb", "spec/controllers/omroep_auth_system_spec.rb"
16
+ m.directory "spec/models"
17
+ m.file "spec/models/omroep_user_spec.rb", "spec/models/omroep_user_spec.rb"
18
+
19
+ m.file "config/initializers/omroep_auth.rb", "config/initializers/omroep_auth.rb"
20
+
21
+ m.file "lib/omroep_auth_system.rb", "lib/omroep_auth_system.rb"
22
+
23
+ m.migration_template "migrate/create_users.rb", "db/migrate"
24
+
25
+ m.readme "INSTALL"
26
+ end
27
+ end
28
+
29
+ def file_name
30
+ 'create_users'
31
+ end
32
+ end
@@ -0,0 +1,2 @@
1
+ require 'lib/omroep_auth_system'
2
+ ActionController::Base.send(:include, OmroepAuthSystem)
@@ -0,0 +1,21 @@
1
+ class OmroepAuthController < ApplicationController
2
+ def new
3
+ end
4
+
5
+ def create
6
+ if user = OmroepUser.authenticate(params[:login], params[:password])
7
+ self.current_user = user
8
+ flash[:notice] = "Je bent ingelogd"
9
+ redirect_to root_path
10
+ else
11
+ @failed_login = true
12
+ render :action => "new"
13
+ end
14
+ end
15
+
16
+ def destroy
17
+ self.current_user = nil
18
+ flash[:notice] = "Je bent uitgelogd"
19
+ redirect_to root_path
20
+ end
21
+ end
@@ -0,0 +1,15 @@
1
+ module OmroepAuthSystem
2
+ def self.included(base)
3
+ base.helper_method :current_user
4
+ end
5
+
6
+ protected
7
+ def current_user
8
+ OmroepUser.find_by_id(session[:user_id])
9
+ end
10
+
11
+ def current_user=(new_user)
12
+ session[:user_id] = new_user ? new_user.id : nil
13
+ @current_user = new_user || false
14
+ end
15
+ end
@@ -0,0 +1,16 @@
1
+ class CreateOmroepUsers < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :omroep_users do |t|
4
+ t.string :login
5
+ t.string :first_name
6
+ t.string :last_name
7
+ t.string :email
8
+ t.datetime :last_login_at
9
+ t.timestamps
10
+ end
11
+ end
12
+
13
+ def self.down
14
+ drop_table :omroep_users
15
+ end
16
+ end
@@ -0,0 +1,45 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+ require 'nokogiri'
4
+
5
+ class OmroepUser < ActiveRecord::Base
6
+ REMOTE_URL = 'http://localhost:3001/remote_login'
7
+
8
+ validates_uniqueness_of :login
9
+
10
+ class << self
11
+ def authenticate(login, password)
12
+ response, body = send_request({ :login => login, :password => password })
13
+ response.code.to_s == '200' ? parse_response(body) : false
14
+ end
15
+
16
+ def from_xml(xml)
17
+ attrs = parse_xml_attributes(xml)
18
+ user = OmroepUser.find_or_create_by_login(attrs[:login])
19
+ user.update_attributes attrs
20
+ user
21
+ end
22
+
23
+ private
24
+ def send_request(params)
25
+ uri = URI.parse(OmroepUser::REMOTE_URL)
26
+ Net::HTTP.post_form(uri, params)
27
+ end
28
+
29
+ def parse_response(body)
30
+ doc = Nokogiri::XML(body)
31
+ from_xml(doc)
32
+ end
33
+
34
+ def parse_xml_attributes(xml)
35
+ attrs = { :last_login_at => Time.now }
36
+
37
+ %w( login first_name last_name email ).each do |key|
38
+ val = xml.search("//#{key}").inner_text
39
+ attrs.merge! key.to_sym => val
40
+ end
41
+
42
+ attrs
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,80 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe OmroepAuthController do
4
+ describe "GET 'new'" do
5
+ it "should render the new template" do
6
+ get 'new'
7
+ response.should render_template('new')
8
+ end
9
+ end
10
+
11
+ describe "POST 'create'" do
12
+ before(:each) do
13
+ @user = mock_model(OmroepUser)
14
+ OmroepUser.stub!(:authenticate).and_return @user
15
+ end
16
+
17
+ def do_post
18
+ post "create", :login => "john", :password => "doe"
19
+ end
20
+
21
+ it "should authenticate the user" do
22
+ OmroepUser.should_receive(:authenticate).with("john", "doe")
23
+ do_post
24
+ end
25
+
26
+ describe "with successfull authentication" do
27
+ it "should set the current user" do
28
+ controller.should_receive(:current_user=).with(@user)
29
+ do_post
30
+ end
31
+
32
+ it "should set the flash" do
33
+ do_post
34
+ flash[:notice].should == "Je bent ingelogd"
35
+ end
36
+
37
+ it "should redirect to the root_path" do
38
+ do_post
39
+ response.should redirect_to(root_path)
40
+ end
41
+ end
42
+
43
+ describe "with failed authentication" do
44
+ before(:each) do
45
+ OmroepUser.stub!(:authenticate).and_return false
46
+ end
47
+
48
+ it "should set a stupid var that needs to be fixed" do
49
+ do_post
50
+ assigns[:failed_login].should be_true
51
+ end
52
+
53
+ it "should render the new template" do
54
+ do_post
55
+ response.should render_template('new')
56
+ end
57
+ end
58
+ end
59
+
60
+ describe "DELETE 'destroy'" do
61
+ def do_delete
62
+ delete "destroy"
63
+ end
64
+
65
+ it "should set the current user to nil" do
66
+ controller.should_receive(:current_user=).with(nil)
67
+ do_delete
68
+ end
69
+
70
+ it "should set the flash" do
71
+ do_delete
72
+ flash[:notice].should == "Je bent uitgelogd"
73
+ end
74
+
75
+ it "should redirect to the root_path" do
76
+ do_delete
77
+ response.should redirect_to(root_path)
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,53 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ require RAILS_ROOT+'/lib/omroep_auth_system'
4
+
5
+ describe "OmroepAuthSystem" do
6
+ class DummyController < ActionController::Base
7
+ include OmroepAuthHelper
8
+
9
+ def test_current_user
10
+ current_user
11
+ end
12
+
13
+ def test_set_current_user
14
+ self.current_user = OmroepUser.find_by_id
15
+ end
16
+
17
+ def test_clear_current_user
18
+ self.current_user = nil
19
+ end
20
+ end
21
+
22
+ controller_name :dummy
23
+
24
+ before(:each) do
25
+ session[:user_id] = 1
26
+ @user = mock_model(OmroepUser, :id => 10)
27
+ OmroepUser.stub!(:find_by_id).and_return @user
28
+
29
+ ActionController::Routing::Routes.draw do |map|
30
+ map.connect "/t_c_u", :controller => 'dummy', :action => 'test_current_user'
31
+ map.connect "/t_s_u", :controller => 'dummy', :action => 'test_set_current_user'
32
+ map.connect "/t_x_u", :controller => 'dummy', :action => 'test_clear_current_user'
33
+ end
34
+ end
35
+
36
+ it "should find the correct omroep user" do
37
+ OmroepUser.should_receive(:find_by_id).with(1)
38
+ get 'test_current_user'
39
+ end
40
+
41
+
42
+ describe "when setting the current user" do
43
+ it "should store the current user id in the session" do
44
+ get 'test_set_current_user'
45
+ session[:user_id].should == 10
46
+ end
47
+
48
+ it "should clear the current user if no user is given" do
49
+ get 'test_clear_current_user'
50
+ session[:user_id].should be_nil
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,67 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ require 'net/http'
4
+ require 'uri'
5
+ require 'nokogiri'
6
+
7
+ describe OmroepUser do
8
+ before(:each) do
9
+ @response = mock("Response", :code => 200)
10
+ @xml = '<user><login>l</login></user>'
11
+ Net::HTTP.stub!(:post_form).and_return [@response, @xml]
12
+ end
13
+
14
+ it "should have the correct REMOTE_URL" do
15
+ OmroepUser::REMOTE_URL.should == 'http://localhost:3001/remote_login'
16
+ end
17
+
18
+ def do_auth(l='login', p='password')
19
+ OmroepUser.authenticate(l, p)
20
+ end
21
+
22
+ describe "when authenticating" do
23
+ it "should send the correct request" do
24
+ uri = URI.parse(OmroepUser::REMOTE_URL)
25
+ Net::HTTP.should_receive(:post_form).with(uri, {:login => 'login', :password => 'password'})
26
+ do_auth
27
+ end
28
+
29
+ it "should generate a new user from the response" do
30
+ Nokogiri.should_receive(:XML).with(@xml).and_return 'parsed xml'
31
+ OmroepUser.should_receive(:from_xml).with('parsed xml')
32
+ do_auth
33
+ end
34
+ end
35
+
36
+ describe "when generating a new user from xml" do
37
+ before(:each) do
38
+ data = "<user><login>l</login><first_name>fn</first_name><last_name>ln</last_name><email>e</email></user>"
39
+ @xml = Nokogiri::XML(data)
40
+
41
+ @t = Time.utc(2009,1,1)
42
+ Time.stub!(:now).and_return @t
43
+
44
+ @user = mock_model(OmroepUser, :update_attributes => true)
45
+ OmroepUser.stub!(:find_or_create_by_login).and_return @user
46
+ end
47
+
48
+ def do_generate
49
+ OmroepUser.from_xml(@xml)
50
+ end
51
+
52
+ it "should find or create a user by login" do
53
+ OmroepUser.should_receive(:find_or_create_by_login).with('l')
54
+ do_generate
55
+ end
56
+
57
+ it "should set the correct attributes on the user" do
58
+ attrs = {:email => 'e', :login => 'l', :first_name => 'fn', :last_name => 'ln', :last_login_at => @t}
59
+ @user.should_receive(:update_attributes).with(attrs)
60
+ do_generate
61
+ end
62
+
63
+ it "should return the user" do
64
+ do_generate.should == @user
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,26 @@
1
+ <% form_tag omroep_remote_login_path do %>
2
+
3
+ <% if @failed_login %>
4
+ <p>
5
+ Ongeldige login of wachtwoord. Probeer het opnieuw.
6
+ </p>
7
+ <% end %>
8
+
9
+ <p class="explanation">
10
+ Log in met je Omroep.nl gegevens.
11
+ </p>
12
+
13
+ <p>
14
+ <%= label_tag 'login', 'Login' %>
15
+ <%= text_field_tag 'login', '', :class => 'text' %>
16
+ </p>
17
+
18
+ <p>
19
+ <%= label_tag 'password', 'Wachtwoord' %>
20
+ <%= password_field_tag 'password', '', :class => 'text password' %>
21
+ </p>
22
+
23
+ <p>
24
+ <%= submit_tag 'Login', :class => 'submit' %>
25
+ </p>
26
+ <% end %>
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: omroep_auth
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bart Zonneveld
@@ -30,6 +30,17 @@ files:
30
30
  - Rakefile
31
31
  - VERSION
32
32
  - lib/omroep_auth.rb
33
+ - omroep_auth.gemspec
34
+ - rails_generators/omroep_auth_generator.rb
35
+ - rails_generators/templates/config/initializers/omroep_auth.rb
36
+ - rails_generators/templates/controllers/omroep_auth_controller.rb
37
+ - rails_generators/templates/lib/omroep_auth_system.rb
38
+ - rails_generators/templates/migrate/create_omroep_users.rb
39
+ - rails_generators/templates/models/omroep_user.rb
40
+ - rails_generators/templates/spec/controllers/omroep_auth_controller_spec.rb
41
+ - rails_generators/templates/spec/controllers/omroep_auth_system_spec.rb
42
+ - rails_generators/templates/spec/models/omroep_user_spec.rb
43
+ - rails_generators/templates/views/omroep_auth/new.html.erb
33
44
  - test/helper.rb
34
45
  - test/test_omroep_auth.rb
35
46
  has_rdoc: true