forked from playframework/play-silhouette
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCasProviderSpec.scala
More file actions
163 lines (135 loc) · 6.08 KB
/
Copy pathCasProviderSpec.scala
File metadata and controls
163 lines (135 loc) · 6.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
* Copyright 2015 Mohiva Organisation (license at mohiva dot com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package play.silhouette.impl.providers
import play.silhouette.api.exceptions.{ ConfigurationException, SilhouetteException }
import play.silhouette.api.util.HTTPLayer
import play.silhouette.api.{ Logger, LoginInfo }
import org.apereo.cas.client.authentication.AttributePrincipal
import org.mockito.Mockito._
import org.specs2.specification.Scope
import play.api.mvc.AnyContentAsEmpty
import play.api.test.FakeRequest
import test.SocialProviderSpec
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import scala.concurrent.duration._
/**
* Test case for the [[CasProvider]] class.
*/
class CasProviderSpec extends SocialProviderSpec[CasInfo] with Logger {
"The settings" should {
"fail with a ConfigurationException if casURL is invalid" in new Context {
CasSettings(casURL = "", redirectURL = "https://cas-redirect/") must throwA[ConfigurationException].like {
case e => e.getMessage must be equalTo CasSettings.CasUrlInvalid.format(CasProvider.ID, "")
}
}
"fail with a ConfigurationException if redirectURL is invalid" in new Context {
CasSettings(casURL = "https://localhost/", redirectURL = "") must throwA[ConfigurationException].like {
case e => e.getMessage must be equalTo CasSettings.RedirectUrlInvalid.format(CasProvider.ID, "")
}
}
"fail with a ConfigurationException if encoding is empty" in new Context {
CasSettings(
casURL = "https://localhost/",
redirectURL = "https://cas-redirect/",
encoding = "") must throwA[ConfigurationException].like {
case e => e.getMessage must be equalTo CasSettings.EncodingInvalid.format(CasProvider.ID)
}
}
"fail with a ConfigurationException if samlTimeTolerance is negative" in new Context {
CasSettings(
casURL = "https://localhost/",
redirectURL = "https://cas-redirect/",
samlTimeTolerance = -1.millis) must throwA[ConfigurationException].like {
case e => e.getMessage must be equalTo CasSettings.TimeToleranceInvalid.format(CasProvider.ID, -1.millis)
}
}
}
"The `authenticate` method" should {
"redirect to CAS server if service ticket is not present in request" in new Context {
implicit val req: FakeRequest[AnyContentAsEmpty.type] = FakeRequest(GET, "/")
result(provider.authenticate()) { result =>
status(result) must equalTo(SEE_OTHER)
redirectLocation(result) must beSome("https://localhost/?service=https%3A%2F%2Fcas-redirect%2F")
}
}
"redirect to CAS server with the original requested URL if service ticket is not present in the request" in new Context {
implicit val req: FakeRequest[AnyContentAsEmpty.type] = FakeRequest(GET, redirectURLWithOrigin)
result(provider.authenticate()) { result =>
status(result) must equalTo(SEE_OTHER)
redirectLocation(result) must beSome("https://localhost/?service=https%3A%2F%2Fcas-redirect%2F")
}
}
"return a valid CASAuthInfo object if service ticket is present in request" in new Context {
implicit val req: FakeRequest[AnyContentAsEmpty.type] = FakeRequest(GET, "/?ticket=%s".format(ticket))
authInfo(provider.authenticate())(authInfo => authInfo must be equalTo CasInfo(ticket))
}
}
"The `retrieveProfile` method" should {
"return a valid profile if the CAS client validates the ticket" in new Context {
when(principal.getName).thenReturn(userName)
when(principal.getAttributes).thenReturn(attr)
doReturn(Future.successful(principal)).when(client).validateServiceTicket(ticket)
implicit val req: FakeRequest[AnyContentAsEmpty.type] = FakeRequest(GET, "/?ticket=%s".format(ticket))
val futureProfile = for {
a <- provider.authenticate()
c <- a.fold(_ => Future.failed(new SilhouetteException("Missing CasInfo")), Future.successful)
p <- provider.retrieveProfile(c)
} yield p
await(futureProfile) must beLike[CommonSocialProfile] {
case profile =>
profile must be equalTo CommonSocialProfile(
loginInfo = new LoginInfo(CasProvider.ID, userName),
firstName = Some(firstName),
lastName = Some(lastName),
fullName = None,
email = Some(email),
avatarURL = Some(pictureURL))
}
}
}
/**
* The context.
*/
trait Context extends Scope {
lazy val settings = CasSettings(
casURL = "https://localhost/",
redirectURL = "https://cas-redirect/")
lazy val httpLayer = {
val m = mock(classOf[HTTPLayer])
when(m.executionContext).thenReturn(global)
m
}
lazy val redirectURLWithOrigin = "%s/my/original/url".format(settings.redirectURL)
lazy val client = spy(new CasClient(settings))
lazy val provider = new CasProvider(httpLayer, settings, client)
lazy val ticket = "ST-12345678"
lazy val casAuthInfo = CasInfo(ticket)
lazy val principal = mock(classOf[AttributePrincipal], withSettings().defaultAnswer(RETURNS_SMART_NULLS))
lazy val name = "abc123"
lazy val email = "email"
lazy val firstName = "Nick"
lazy val lastName = "Shaw"
lazy val userName = "314159"
lazy val pictureURL = "http://www.gravatar"
lazy val attr = new java.util.HashMap[String, Object]()
attr.put(CasProvider.Email, email)
attr.put(CasProvider.FirstName, firstName)
attr.put(CasProvider.LastName, lastName)
attr.put(CasProvider.UserName, userName)
attr.put(CasProvider.PictureURL, pictureURL)
}
}