|
1 | 1 | describe SessionsController do |
2 | 2 | before(:each) { request.env['HTTP_REFERER'] = 'source_page' } |
| 3 | + let(:success) { |
| 4 | + '<?xml version="1.0" encoding="UTF-8"?> |
| 5 | + <cas:serviceResponse xmlns:cas="http://www.yale.edu/tp/cas"> |
| 6 | + <cas:authenticationSuccess> |
| 7 | + <cas:user>web_admin</cas:user> |
| 8 | + </cas:authenticationSuccess> |
| 9 | + </cas:serviceResponse>' } |
| 10 | + let(:failure) { |
| 11 | + '<?xml version="1.0" encoding="UTF-8"?> |
| 12 | + <cas:serviceResponse xmlns:cas="http://www.yale.edu/tp/cas"> |
| 13 | + <cas:authenticationFailure code=''INVALID_TICKET''> |
| 14 | + E_TICKET_EXPIRED |
| 15 | + </cas:authenticationFailure> |
| 16 | + </cas:serviceResponse>' } |
| 17 | + let(:secure_root_url) { root_url(protocol: :https) } |
3 | 18 |
|
4 | | - describe "#cas_reg" do |
5 | | - it "returns a cas-reg URL" do |
6 | | - expect(SessionsController.new.cas_reg).to match /https.*cas-reg/ |
7 | | - end |
8 | | - end |
9 | | - |
10 | | - describe "#cas" do |
11 | | - it "returns a cas URL" do |
12 | | - expect(SessionsController.new.cas_reg).to match /https.*cas/ |
| 19 | + describe "url helpers" do |
| 20 | + describe "#cas" do |
| 21 | + context "with no arguments" do |
| 22 | + it "returns a cas service root URL" do |
| 23 | + expect(SessionsController.new.send(:cas_url)).to match /https.*login/ |
| 24 | + end |
| 25 | + end |
| 26 | + context "with invalid action" do |
| 27 | + it "returns a cas service root URL" do |
| 28 | + expect(SessionsController.new.send(:cas_url, :foo)).to eq SessionsController.new.send(:cas_url) |
| 29 | + end |
| 30 | + end |
| 31 | + context "with :logout" do |
| 32 | + it "returns a cas service logout URL" do |
| 33 | + expect(SessionsController.new.send(:cas_url, :logout)).to match /https.*login.*logout/ |
| 34 | + end |
| 35 | + end |
| 36 | + context "with :login" do |
| 37 | + it "returns a cas service login URL" do |
| 38 | + expect(SessionsController.new.send(:cas_url, :login, service: 'foo')).to match /https.*login.*login.*foo/ |
| 39 | + end |
| 40 | + end |
| 41 | + context "with :validate" do |
| 42 | + it "returns a cas service login URL" do |
| 43 | + expect(SessionsController.new.send(:cas_url, :validate, ticket: 'foo', service: 'bar')).to match /https.*login.*serviceValidate.*foo.*bar/ |
| 44 | + end |
| 45 | + end |
13 | 46 | end |
14 | 47 | end |
15 | 48 |
|
16 | 49 | describe "#new" do |
17 | 50 | before(:each) do |
18 | | - stub_request(:get, /cas.iu.edu/). |
| 51 | + stub_request(:get, /idp-stg.login.iu.edu/). |
19 | 52 | with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}). |
20 | | - to_return(status: 200, body: "yes", headers: {}) |
| 53 | + to_return(status: 200, body: success, headers: {}) |
21 | 54 | end |
22 | 55 | before(:each) { get :new } |
23 | | - let(:cas) { "https://cas.iu.edu" } |
24 | 56 | it "redirects to cas login" do |
25 | | - expect(response).to redirect_to "#{cas}/cas/login?cassvc=ANY&casurl=#{root_url}sessions/validate_login" |
| 57 | + expect(response).to redirect_to SessionsController.new.send(:cas_url, :login, service: secure_root_url) |
26 | 58 | end |
27 | 59 | end |
28 | 60 | describe "#validate_login" do |
29 | | - context "with invalid casticket" do |
| 61 | + context "with invalid ticket" do |
30 | 62 | before(:each) do |
31 | | - stub_request(:get, /cas.iu.edu/). |
| 63 | + stub_request(:get, /idp.*login.iu.edu/). |
32 | 64 | with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}). |
33 | | - to_return(status: 200, body: "no", headers: {}) |
| 65 | + to_return(status: 200, body: failure, headers: {}) |
34 | 66 | end |
35 | | - before(:each) { get :validate_login, casticket: casticket } |
36 | | - let(:casticket) { 'foo' } |
37 | | - it "assigns @casticket" do |
38 | | - expect(assigns(:casticket)).to eq casticket |
| 67 | + before(:each) { get :validate_login, ticket: cas_ticket } |
| 68 | + let(:cas_ticket) { 'foo' } |
| 69 | + it "assigns @cas_ticket" do |
| 70 | + expect(assigns(:cas_ticket)).to eq cas_ticket |
39 | 71 | end |
40 | | - it "assigns @resp=no" do |
41 | | - expect(assigns(:resp)).to match /^no/ |
| 72 | + it "assigns @cas_user blank" do |
| 73 | + expect(assigns(:cas_user)).to be_blank |
42 | 74 | end |
43 | 75 | it "redirects to denial page" do |
44 | | - expect(response).to redirect_to "#{root_url}denied.html" |
| 76 | + expect(response).to redirect_to "#{secure_root_url}denied.html" |
45 | 77 | end |
46 | 78 | end |
47 | | - context "with valid casticket" do |
| 79 | + context "with invalid response" do |
48 | 80 | before(:each) do |
49 | | - stub_request(:get, /cas.iu.edu/). |
| 81 | + stub_request(:get, /idp.*login.iu.edu/). |
50 | 82 | with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}). |
51 | | - to_return(status: 200, body: "yes--web_admin--", headers: {}) |
| 83 | + to_return(status: 200, body: 'non-parsing response', headers: {}) |
| 84 | + end |
| 85 | + before(:each) { get :validate_login, ticket: cas_ticket } |
| 86 | + let(:cas_ticket) { 'foo' } |
| 87 | + it "assigns @cas_ticket" do |
| 88 | + expect(assigns(:cas_ticket)).to eq cas_ticket |
| 89 | + end |
| 90 | + it "assigns @cas_user blank" do |
| 91 | + expect(assigns(:cas_user)).to be_blank |
| 92 | + end |
| 93 | + it "redirects to denial page" do |
| 94 | + expect(response).to redirect_to "#{secure_root_url}denied.html" |
| 95 | + end |
| 96 | + end |
| 97 | + context "with valid ticket" do |
| 98 | + before(:each) do |
| 99 | + stub_request(:get, /idp.*login.iu.edu/). |
| 100 | + with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}). |
| 101 | + to_return(status: 200, body: success, headers: {}) |
| 102 | + end |
| 103 | + before(:each) { get :validate_login, ticket: cas_ticket } |
| 104 | + after(:each) do |
| 105 | + sign_out |
| 106 | + Thread.current[:current_username] = nil |
52 | 107 | end |
53 | | - before(:each) { get :validate_login, casticket: casticket } |
54 | | - let(:casticket) { 'valid' } |
55 | | - it "assigns @casticket" do |
56 | | - expect(assigns(:casticket)).to eq casticket |
| 108 | + let(:cas_ticket) { 'valid' } |
| 109 | + it "assigns @cas_ticket" do |
| 110 | + expect(assigns(:cas_ticket)).to eq cas_ticket |
57 | 111 | end |
58 | | - it "assigns @resp=yes" do |
59 | | - expect(assigns(:resp)).to match /^yes/ |
| 112 | + it "assigns @cas_user" do |
| 113 | + expect(assigns(:cas_user)).not_to be_blank |
60 | 114 | end |
61 | | - it "redirects to root_url" do |
62 | | - expect(response).to redirect_to root_url |
| 115 | + it "redirects to secure_root_url" do |
| 116 | + expect(response).to redirect_to secure_root_url |
63 | 117 | end |
64 | 118 | end |
65 | 119 | end |
|
72 | 126 | destroy_session |
73 | 127 | expect(session[:username]).to be_nil |
74 | 128 | end |
75 | | - it "redirects to 'https://cas.iu.edu/cas/logout'" do |
| 129 | + it "redirects to logout service'" do |
76 | 130 | destroy_session |
77 | | - expect(response).to redirect_to 'https://cas.iu.edu/cas/logout' |
| 131 | + expect(response).to redirect_to SessionsController.new.send(:cas_url, :logout) |
78 | 132 | end |
79 | 133 | end |
80 | 134 |
|
|
0 commit comments