PageRenderTime 44ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/test/Microsoft.Web.WebPages.OAuth.Test/OAuthWebSecurityTest.cs

https://bitbucket.org/mdavid/aspnetwebstack
C# | 389 lines | 271 code | 75 blank | 43 comment | 2 complexity | e6e7250853741d633f26c6e7ce0294c4 MD5 | raw file
  1. using System;
  2. using System.Collections.Specialized;
  3. using System.Web;
  4. using System.Web.Security;
  5. using DotNetOpenAuth.AspNet;
  6. using Microsoft.TestCommon;
  7. using Moq;
  8. using Xunit;
  9. namespace Microsoft.Web.WebPages.OAuth.Test
  10. {
  11. public class OAuthWebSecurityTest : IDisposable
  12. {
  13. [Fact]
  14. public void RegisterClientThrowsOnNullValue()
  15. {
  16. AssertEx.ThrowsArgumentNull(() => OAuthWebSecurity.RegisterClient(null), "client");
  17. }
  18. [Fact]
  19. public void RegisterClientThrowsIfProviderNameIsEmpty()
  20. {
  21. // Arrange
  22. var client = new Mock<IAuthenticationClient>();
  23. client.Setup(c => c.ProviderName).Returns((string)null);
  24. // Act & Assert
  25. AssertEx.ThrowsArgument(() => OAuthWebSecurity.RegisterClient(client.Object), "client");
  26. client.Setup(c => c.ProviderName).Returns("");
  27. // Act & Assert
  28. AssertEx.ThrowsArgument(() => OAuthWebSecurity.RegisterClient(client.Object), "client");
  29. }
  30. [Fact]
  31. public void RegisterClientThrowsRegisterMoreThanOneClientWithTheSameName()
  32. {
  33. // Arrange
  34. var client1 = new Mock<IAuthenticationClient>();
  35. client1.Setup(c => c.ProviderName).Returns("provider");
  36. var client2 = new Mock<IAuthenticationClient>();
  37. client2.Setup(c => c.ProviderName).Returns("provider");
  38. OAuthWebSecurity.RegisterClient(client1.Object);
  39. // Act & Assert
  40. AssertEx.ThrowsArgument(() => OAuthWebSecurity.RegisterClient(client2.Object), null);
  41. }
  42. [Fact]
  43. public void RegisterOAuthClient()
  44. {
  45. // Arrange
  46. var clients = new BuiltInOAuthClient[]
  47. {
  48. BuiltInOAuthClient.Facebook,
  49. BuiltInOAuthClient.Twitter,
  50. BuiltInOAuthClient.LinkedIn,
  51. BuiltInOAuthClient.WindowsLive
  52. };
  53. var clientNames = new string[]
  54. {
  55. "Facebook",
  56. "Twitter",
  57. "LinkedIn",
  58. "WindowsLive"
  59. };
  60. for (int i = 0; i < clients.Length; i++)
  61. {
  62. // Act
  63. OAuthWebSecurity.RegisterOAuthClient(clients[i], "key", "secret");
  64. var client = new Mock<IAuthenticationClient>();
  65. client.Setup(c => c.ProviderName).Returns(clientNames[i]);
  66. // Assert
  67. Assert.Throws(typeof(ArgumentException), () => OAuthWebSecurity.RegisterClient(client.Object));
  68. }
  69. }
  70. [Fact]
  71. public void RegisterOpenIDClient()
  72. {
  73. // Arrange
  74. var clients = new BuiltInOpenIDClient[]
  75. {
  76. BuiltInOpenIDClient.Google,
  77. BuiltInOpenIDClient.Yahoo
  78. };
  79. var clientNames = new string[]
  80. {
  81. "Google",
  82. "Yahoo"
  83. };
  84. for (int i = 0; i < clients.Length; i++)
  85. {
  86. // Act
  87. OAuthWebSecurity.RegisterOpenIDClient(clients[i]);
  88. var client = new Mock<IAuthenticationClient>();
  89. client.Setup(c => c.ProviderName).Returns(clientNames[i]);
  90. // Assert
  91. AssertEx.ThrowsArgument(() => OAuthWebSecurity.RegisterClient(client.Object), null);
  92. }
  93. }
  94. [Fact]
  95. public void RequestAuthenticationRedirectsToProviderWithNullReturnUrl()
  96. {
  97. // Arrange
  98. var context = new Mock<HttpContextBase>();
  99. context.Setup(c => c.Request.ServerVariables).Returns(
  100. new NameValueCollection());
  101. context.Setup(c => c.Request.Url).Returns(new Uri("http://live.com/login.aspx"));
  102. context.Setup(c => c.Request.RawUrl).Returns("/login.aspx");
  103. var client = new Mock<IAuthenticationClient>();
  104. client.Setup(c => c.ProviderName).Returns("windowslive");
  105. client.Setup(c => c.RequestAuthentication(
  106. context.Object,
  107. It.Is<Uri>(u => u.AbsoluteUri.Equals("http://live.com/login.aspx?__provider__=windowslive", StringComparison.OrdinalIgnoreCase))))
  108. .Verifiable();
  109. OAuthWebSecurity.RegisterClient(client.Object);
  110. // Act
  111. OAuthWebSecurity.RequestAuthenticationCore(context.Object, "windowslive", null);
  112. // Assert
  113. client.Verify();
  114. }
  115. [Fact]
  116. public void RequestAuthenticationRedirectsToProviderWithReturnUrl()
  117. {
  118. // Arrange
  119. var context = new Mock<HttpContextBase>();
  120. context.Setup(c => c.Request.ServerVariables).Returns(
  121. new NameValueCollection());
  122. context.Setup(c => c.Request.Url).Returns(new Uri("http://live.com/login.aspx"));
  123. context.Setup(c => c.Request.RawUrl).Returns("/login.aspx");
  124. var client = new Mock<IAuthenticationClient>();
  125. client.Setup(c => c.ProviderName).Returns("yahoo");
  126. client.Setup(c => c.RequestAuthentication(
  127. context.Object,
  128. It.Is<Uri>(u => u.AbsoluteUri.Equals("http://yahoo.com/?__provider__=yahoo", StringComparison.OrdinalIgnoreCase))))
  129. .Verifiable();
  130. OAuthWebSecurity.RegisterClient(client.Object);
  131. // Act
  132. OAuthWebSecurity.RequestAuthenticationCore(context.Object, "yahoo", "http://yahoo.com");
  133. // Assert
  134. client.Verify();
  135. }
  136. [Fact]
  137. public void VerifyAuthenticationSucceed()
  138. {
  139. // Arrange
  140. var queryStrings = new NameValueCollection();
  141. queryStrings.Add("__provider__", "facebook");
  142. var context = new Mock<HttpContextBase>();
  143. context.Setup(c => c.Request.QueryString).Returns(queryStrings);
  144. var client = new Mock<IAuthenticationClient>(MockBehavior.Strict);
  145. client.Setup(c => c.ProviderName).Returns("facebook");
  146. client.Setup(c => c.VerifyAuthentication(context.Object)).Returns(new AuthenticationResult(true, "facebook", "123",
  147. "super", null));
  148. var anotherClient = new Mock<IAuthenticationClient>(MockBehavior.Strict);
  149. anotherClient.Setup(c => c.ProviderName).Returns("twitter");
  150. anotherClient.Setup(c => c.VerifyAuthentication(context.Object)).Returns(AuthenticationResult.Failed);
  151. OAuthWebSecurity.RegisterClient(client.Object);
  152. OAuthWebSecurity.RegisterClient(anotherClient.Object);
  153. // Act
  154. AuthenticationResult result = OAuthWebSecurity.VerifyAuthenticationCore(context.Object);
  155. // Assert
  156. Assert.True(result.IsSuccessful);
  157. Assert.Equal("facebook", result.Provider);
  158. Assert.Equal("123", result.ProviderUserId);
  159. Assert.Equal("super", result.UserName);
  160. Assert.Null(result.Error);
  161. Assert.Null(result.ExtraData);
  162. }
  163. [Fact]
  164. public void VerifyAuthenticationFail()
  165. {
  166. // Arrange
  167. var queryStrings = new NameValueCollection();
  168. queryStrings.Add("__provider__", "twitter");
  169. var context = new Mock<HttpContextBase>();
  170. context.Setup(c => c.Request.QueryString).Returns(queryStrings);
  171. var client = new Mock<IAuthenticationClient>(MockBehavior.Strict);
  172. client.Setup(c => c.ProviderName).Returns("facebook");
  173. client.Setup(c => c.VerifyAuthentication(context.Object)).Returns(new AuthenticationResult(true, "facebook", "123",
  174. "super", null));
  175. var anotherClient = new Mock<IAuthenticationClient>(MockBehavior.Strict);
  176. anotherClient.Setup(c => c.ProviderName).Returns("twitter");
  177. anotherClient.Setup(c => c.VerifyAuthentication(context.Object)).Returns(AuthenticationResult.Failed);
  178. OAuthWebSecurity.RegisterClient(client.Object);
  179. OAuthWebSecurity.RegisterClient(anotherClient.Object);
  180. // Act
  181. AuthenticationResult result = OAuthWebSecurity.VerifyAuthenticationCore(context.Object);
  182. // Assert
  183. Assert.False(result.IsSuccessful);
  184. Assert.Equal("twitter", result.Provider);
  185. }
  186. [Fact]
  187. public void VerifyAuthenticationFailIfNoProviderInQueryString()
  188. {
  189. // Arrange
  190. var context = new Mock<HttpContextBase>();
  191. context.Setup(c => c.Request.QueryString).Returns(new NameValueCollection());
  192. var client = new Mock<IAuthenticationClient>(MockBehavior.Strict);
  193. client.Setup(c => c.ProviderName).Returns("facebook");
  194. var anotherClient = new Mock<IAuthenticationClient>(MockBehavior.Strict);
  195. anotherClient.Setup(c => c.ProviderName).Returns("twitter");
  196. OAuthWebSecurity.RegisterClient(client.Object);
  197. OAuthWebSecurity.RegisterClient(anotherClient.Object);
  198. // Act
  199. AuthenticationResult result = OAuthWebSecurity.VerifyAuthenticationCore(context.Object);
  200. // Assert
  201. Assert.False(result.IsSuccessful);
  202. Assert.Null(result.Provider);
  203. }
  204. [Fact]
  205. public void LoginSetAuthenticationTicketIfSuccessful()
  206. {
  207. // Arrange
  208. var cookies = new HttpCookieCollection();
  209. var context = new Mock<HttpContextBase>();
  210. context.Setup(c => c.Request.IsSecureConnection).Returns(true);
  211. context.Setup(c => c.Response.Cookies).Returns(cookies);
  212. var dataProvider = new Mock<IOpenAuthDataProvider>(MockBehavior.Strict);
  213. dataProvider.Setup(p => p.GetUserNameFromOpenAuth("twitter", "12345")).Returns("hola");
  214. OAuthWebSecurity.OAuthDataProvider = dataProvider.Object;
  215. OAuthWebSecurity.RegisterOAuthClient(BuiltInOAuthClient.Twitter, "sdfdsfsd", "dfdsfdsf");
  216. // Act
  217. bool successful = OAuthWebSecurity.LoginCore(context.Object, "twitter", "12345", createPersistentCookie: false);
  218. // Assert
  219. Assert.True(successful);
  220. Assert.Equal(1, cookies.Count);
  221. HttpCookie addedCookie = cookies[0];
  222. Assert.Equal(FormsAuthentication.FormsCookieName, addedCookie.Name);
  223. Assert.True(addedCookie.HttpOnly);
  224. Assert.Equal("/", addedCookie.Path);
  225. Assert.False(addedCookie.Secure);
  226. Assert.False(String.IsNullOrEmpty(addedCookie.Value));
  227. FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(addedCookie.Value);
  228. Assert.NotNull(ticket);
  229. Assert.Equal(2, ticket.Version);
  230. Assert.Equal("hola", ticket.Name);
  231. Assert.Equal("OAuth", ticket.UserData);
  232. Assert.False(ticket.IsPersistent);
  233. }
  234. [Fact]
  235. public void LoginFailIfUserIsNotFound()
  236. {
  237. // Arrange
  238. var context = new Mock<HttpContextBase>();
  239. OAuthWebSecurity.RegisterOAuthClient(BuiltInOAuthClient.Twitter, "consumerKey", "consumerSecrte");
  240. var dataProvider = new Mock<IOpenAuthDataProvider>();
  241. dataProvider.Setup(p => p.GetUserNameFromOpenAuth("twitter", "12345")).Returns((string)null);
  242. OAuthWebSecurity.OAuthDataProvider = dataProvider.Object;
  243. // Act
  244. bool successful = OAuthWebSecurity.LoginCore(context.Object, "twitter", "12345", createPersistentCookie: false);
  245. // Assert
  246. Assert.False(successful);
  247. }
  248. [Fact]
  249. public void GetOAuthClientReturnsTheCorrectClient()
  250. {
  251. // Arrange
  252. var client = new Mock<IAuthenticationClient>();
  253. client.Setup(c => c.ProviderName).Returns("facebook");
  254. OAuthWebSecurity.RegisterClient(client.Object);
  255. var anotherClient = new Mock<IAuthenticationClient>();
  256. anotherClient.Setup(c => c.ProviderName).Returns("hulu");
  257. OAuthWebSecurity.RegisterClient(anotherClient.Object);
  258. // Act
  259. var expectedClient = OAuthWebSecurity.GetOAuthClient("facebook");
  260. // Assert
  261. Assert.Same(expectedClient, client.Object);
  262. }
  263. [Fact]
  264. public void GetOAuthClientThrowsIfClientIsNotFound()
  265. {
  266. // Arrange
  267. var client = new Mock<IAuthenticationClient>();
  268. client.Setup(c => c.ProviderName).Returns("facebook");
  269. OAuthWebSecurity.RegisterClient(client.Object);
  270. var anotherClient = new Mock<IAuthenticationClient>();
  271. anotherClient.Setup(c => c.ProviderName).Returns("hulu");
  272. OAuthWebSecurity.RegisterClient(anotherClient.Object);
  273. // Act & Assert
  274. Assert.Throws<ArgumentException>(() => OAuthWebSecurity.GetOAuthClient("live"));
  275. }
  276. [Fact]
  277. public void TryGetOAuthClientSucceeds()
  278. {
  279. // Arrange
  280. var client = new Mock<IAuthenticationClient>();
  281. client.Setup(c => c.ProviderName).Returns("facebook");
  282. OAuthWebSecurity.RegisterClient(client.Object);
  283. var anotherClient = new Mock<IAuthenticationClient>();
  284. anotherClient.Setup(c => c.ProviderName).Returns("hulu");
  285. OAuthWebSecurity.RegisterClient(anotherClient.Object);
  286. // Act
  287. IAuthenticationClient expectedClient;
  288. bool result = OAuthWebSecurity.TryGetOAuthClient("facebook", out expectedClient);
  289. // Assert
  290. Assert.Same(expectedClient, client.Object);
  291. Assert.True(result);
  292. }
  293. [Fact]
  294. public void TryGetOAuthClientFail()
  295. {
  296. // Arrange
  297. var client = new Mock<IAuthenticationClient>();
  298. client.Setup(c => c.ProviderName).Returns("facebook");
  299. OAuthWebSecurity.RegisterClient(client.Object);
  300. var anotherClient = new Mock<IAuthenticationClient>();
  301. anotherClient.Setup(c => c.ProviderName).Returns("hulu");
  302. OAuthWebSecurity.RegisterClient(anotherClient.Object);
  303. // Act
  304. IAuthenticationClient expectedClient;
  305. bool result = OAuthWebSecurity.TryGetOAuthClient("live", out expectedClient);
  306. // Assert
  307. Assert.Null(expectedClient);
  308. Assert.False(result);
  309. }
  310. public void Dispose() {
  311. OAuthWebSecurity.ClearProviders();
  312. }
  313. }
  314. }