Skip to content

Commit 80b0556

Browse files
author
Elad Zelingher
committed
More cases for Router-side authentication tests
1 parent 3e7a191 commit 80b0556

File tree

1 file changed

+226
-0
lines changed

1 file changed

+226
-0
lines changed

src/net45/Tests/WampSharp.Tests.Wampv2/Integration/AuthenticationServerTests.cs

Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,120 @@ public void HelloParametersArePassedToAuthenticationFactory()
4949
Assert.That(authenticatorFactoryParameters.HelloDetails.AuthenticationId, Is.EqualTo("joe"));
5050
}
5151

52+
[Test]
53+
public void ExceptionOnGetSessionAuthenticatorRaisesAbort()
54+
{
55+
MockSessionAuthenticationFactory mockSessionAuthenticationFactory =
56+
new MockSessionAuthenticationFactory();
57+
58+
WampPendingClientDetails authenticatorFactoryParameters = null;
59+
60+
mockSessionAuthenticationFactory.SetGetSessionAuthenticator
61+
((clientDetails, transportAuthenticator) =>
62+
{
63+
throw new WampAuthenticationException(new MyAbortDetails() { Message = "aborted!", Year = 2015 }, "com.myapp.abortreason");
64+
});
65+
66+
WampAuthenticationPlayground playground =
67+
new WampAuthenticationPlayground(mockSessionAuthenticationFactory);
68+
69+
playground.Host.Open();
70+
71+
string clientReason = null;
72+
AbortDetails clientAbortDetails = null;
73+
74+
Mock<IWampClient<JToken>> clientMock = new Mock<IWampClient<JToken>>();
75+
76+
clientMock.Setup(x => x.Abort(It.IsAny<AbortDetails>(), It.IsAny<string>()))
77+
.Callback((AbortDetails details, string reason) =>
78+
{
79+
clientReason = reason;
80+
clientAbortDetails = details;
81+
});
82+
83+
IWampServerProxy serverProxy =
84+
playground.CreateRawConnection(clientMock.Object);
85+
86+
serverProxy.Hello("realm1", new HelloDetailsHack()
87+
{
88+
AuthenticationId = "joe",
89+
AuthenticationMethods = new string[] { "wampcra", "ticket" }
90+
});
91+
92+
Assert.That(clientReason, Is.EqualTo("com.myapp.abortreason"));
93+
Assert.That(clientAbortDetails.Message, Is.EqualTo("aborted!"));
94+
95+
var deserialized =
96+
clientAbortDetails.OriginalValue.Deserialize<MyAbortDetails>();
97+
98+
Assert.That(deserialized.Year, Is.EqualTo(2015));
99+
}
100+
101+
[Test]
102+
public void IsAuthenticatedTrueOnGetSessionAuthenticatorRaisesWelcome()
103+
{
104+
MockSessionAuthenticationFactory mockSessionAuthenticationFactory =
105+
new MockSessionAuthenticationFactory();
106+
107+
WampPendingClientDetails authenticatorFactoryParameters = null;
108+
109+
mockSessionAuthenticationFactory.SetGetSessionAuthenticator
110+
((clientDetails, transportAuthenticator) =>
111+
{
112+
authenticatorFactoryParameters = clientDetails;
113+
MockSessionAuthenticator mockSessionAuthenticator = new MockSessionAuthenticator();
114+
mockSessionAuthenticator.SetAuthenticationMethod("anonymous");
115+
mockSessionAuthenticator.SetAuthenticationId(clientDetails.HelloDetails.AuthenticationId);
116+
mockSessionAuthenticator.SetAuthorizer(new WampStaticAuthorizer(new List<WampUriPermissions>()));
117+
mockSessionAuthenticator.SetWelcomeDetails(new MyWelcomeDetails()
118+
{
119+
Country = "United States of America",
120+
AuthenticationProvider = "unittest",
121+
AuthenticationRole = "testee"
122+
});
123+
mockSessionAuthenticator.SetIsAuthenticated(true);
124+
125+
return mockSessionAuthenticator;
126+
});
127+
128+
WampAuthenticationPlayground playground =
129+
new WampAuthenticationPlayground(mockSessionAuthenticationFactory);
130+
131+
playground.Host.Open();
132+
133+
long? clientSession = null;
134+
WelcomeDetails clientWelcomeDetails = null;
135+
136+
Mock<IWampClient<JToken>> clientMock = new Mock<IWampClient<JToken>>();
137+
138+
clientMock.Setup(x => x.Welcome(It.IsAny<long>(), It.IsAny<WelcomeDetails>()))
139+
.Callback((long session, WelcomeDetails details) =>
140+
{
141+
clientWelcomeDetails = details;
142+
clientSession = session;
143+
});
144+
145+
IWampServerProxy serverProxy =
146+
playground.CreateRawConnection(clientMock.Object);
147+
148+
serverProxy.Hello("realm1", new HelloDetailsHack()
149+
{
150+
AuthenticationId = "joe",
151+
AuthenticationMethods = new string[] { "wampcra", "ticket" }
152+
});
153+
154+
Assert.That(clientWelcomeDetails.AuthenticationMethod, Is.EqualTo("anonymous"));
155+
Assert.That(clientWelcomeDetails.AuthenticationId, Is.EqualTo("joe"));
156+
Assert.That(clientWelcomeDetails.AuthenticationProvider, Is.EqualTo("unittest"));
157+
Assert.That(clientWelcomeDetails.AuthenticationRole, Is.EqualTo("testee"));
158+
Assert.That(clientSession, Is.EqualTo(authenticatorFactoryParameters.SessionId));
159+
160+
MyWelcomeDetails deserializedWelcomeDetails =
161+
clientWelcomeDetails.OriginalValue.Deserialize<MyWelcomeDetails>();
162+
163+
Assert.That(deserializedWelcomeDetails.Country, Is.EqualTo("United States of America"));
164+
}
165+
52166
[Test]
53167
public void ChallengeParametersArePassedToClient()
54168
{
@@ -154,6 +268,112 @@ public void AuthenticateParametersArePassedToSessionAuthenticator()
154268
Assert.That(deserializedExtraData.Wife, Is.EqualTo("Michelle"));
155269
}
156270

271+
[Test]
272+
public void ExceptionOnAuthenticateRaisesAbort()
273+
{
274+
MockSessionAuthenticationFactory mockSessionAuthenticationFactory =
275+
new MockSessionAuthenticationFactory();
276+
277+
WampPendingClientDetails authenticatorFactoryParameters = null;
278+
279+
mockSessionAuthenticationFactory.SetGetSessionAuthenticator
280+
((clientDetails, transportAuthenticator) =>
281+
{
282+
authenticatorFactoryParameters = clientDetails;
283+
MockSessionAuthenticator mockSessionAuthenticator = new MockSessionAuthenticator();
284+
mockSessionAuthenticator.SetAuthenticationMethod("ticket");
285+
286+
mockSessionAuthenticator.SetAuthenticate((signature, extraData) =>
287+
{
288+
throw new WampAuthenticationException(new MyAbortDetails() {Message = "aborted!", Year = 2015}, "com.myapp.abortreason");
289+
});
290+
291+
return mockSessionAuthenticator;
292+
});
293+
294+
WampAuthenticationPlayground playground =
295+
new WampAuthenticationPlayground(mockSessionAuthenticationFactory);
296+
297+
playground.Host.Open();
298+
299+
string clientReason = null;
300+
AbortDetails clientAbortDetails = null;
301+
302+
Mock<IWampClient<JToken>> clientMock = new Mock<IWampClient<JToken>>();
303+
304+
clientMock.Setup(x => x.Abort(It.IsAny<AbortDetails>(), It.IsAny<string>()))
305+
.Callback((AbortDetails details, string reason) =>
306+
{
307+
clientReason = reason;
308+
clientAbortDetails = details;
309+
});
310+
311+
IWampServerProxy serverProxy =
312+
playground.CreateRawConnection(clientMock.Object);
313+
314+
serverProxy.Hello("realm1", new HelloDetailsHack()
315+
{
316+
AuthenticationId = "joe",
317+
AuthenticationMethods = new string[] { "wampcra", "ticket" }
318+
});
319+
320+
serverProxy.Authenticate("Barack Hussein", new AuthenticateExtraData());
321+
322+
Assert.That(clientReason, Is.EqualTo("com.myapp.abortreason"));
323+
Assert.That(clientAbortDetails.Message, Is.EqualTo("aborted!"));
324+
325+
var deserialized =
326+
clientAbortDetails.OriginalValue.Deserialize<MyAbortDetails>();
327+
328+
Assert.That(deserialized.Year, Is.EqualTo(2015));
329+
}
330+
331+
[Test]
332+
public void NotAuthenticatedRaisesAbort()
333+
{
334+
MockSessionAuthenticationFactory mockSessionAuthenticationFactory =
335+
new MockSessionAuthenticationFactory();
336+
337+
WampPendingClientDetails authenticatorFactoryParameters = null;
338+
339+
mockSessionAuthenticationFactory.SetGetSessionAuthenticator
340+
((clientDetails, transportAuthenticator) =>
341+
{
342+
authenticatorFactoryParameters = clientDetails;
343+
MockSessionAuthenticator mockSessionAuthenticator = new MockSessionAuthenticator();
344+
mockSessionAuthenticator.SetAuthenticationMethod("ticket");
345+
346+
mockSessionAuthenticator.SetAuthenticate((signature, extraData) =>
347+
{
348+
mockSessionAuthenticator.SetAuthenticationId(clientDetails.HelloDetails.AuthenticationId);
349+
mockSessionAuthenticator.SetIsAuthenticated(false);
350+
});
351+
352+
return mockSessionAuthenticator;
353+
});
354+
355+
WampAuthenticationPlayground playground =
356+
new WampAuthenticationPlayground(mockSessionAuthenticationFactory);
357+
358+
playground.Host.Open();
359+
360+
Mock<IWampClient<JToken>> clientMock = new Mock<IWampClient<JToken>>();
361+
362+
IWampServerProxy serverProxy =
363+
playground.CreateRawConnection(clientMock.Object);
364+
365+
serverProxy.Hello("realm1", new HelloDetailsHack()
366+
{
367+
AuthenticationId = "joe",
368+
AuthenticationMethods = new string[] {"wampcra", "ticket"}
369+
});
370+
371+
serverProxy.Authenticate("Barack Hussein", new AuthenticateExtraData());
372+
373+
clientMock.Verify(x => x.Abort(It.IsAny<AbortDetails>(), It.IsAny<string>()));
374+
}
375+
376+
157377
[Test]
158378
public void WelcomeParametersArePassedToClient()
159379
{
@@ -342,5 +562,11 @@ private class MyWelcomeDetails : WelcomeDetails
342562
[JsonProperty("country")]
343563
public string Country { get; set; }
344564
}
565+
566+
private class MyAbortDetails : AbortDetails
567+
{
568+
[JsonProperty("year")]
569+
public int Year { get; set; }
570+
}
345571
}
346572
}

0 commit comments

Comments
 (0)