public void TestProcessTokenRequest() { AccessTokenBase token = new AccessTokenBase { Scope = new string[] { "create", "delete" }, ExpiresIn = 120, RefreshToken = "refresh", Token = "token", TokenType = "bearer" }; ClientBase client = new ClientBase { ClientId = "123", ClientSecret = "secret" }; IAuthorizationGrant grant = new AuthorizationGrantBase { Code = "123" }; AuthorizationContext context = new AuthorizationContext { Client = new ClientBase { ClientId = "123" }, IsApproved = true, RedirectUri = new Uri("http://www.mysite.com/callback"), Scope = new string[] { "create", "delete" }, ResourceOwnerUsername = "******" }; Mock <IClientService> mckClientService = new Mock <IClientService>(); mckClientService.Setup(x => x.FindClient("123")).Returns(client); Mock <IAuthorizationGrantService> mckGrantService = new Mock <IAuthorizationGrantService>(); mckGrantService.Setup(x => x.IssueAuthorizationGrant(context)).Returns(grant); Mock <ITokenService> mckTokenService = new Mock <ITokenService>(); mckTokenService.Setup(x => x.IssueAccessToken(grant)).Returns(token); Mock <IServiceFactory> mckFactory = new Mock <IServiceFactory>(); mckFactory.SetupGet(x => x.TokenService).Returns(mckTokenService.Object); mckFactory.SetupGet(x => x.ClientService).Returns(mckClientService.Object); mckFactory.SetupGet(x => x.AuthorizationGrantService).Returns(mckGrantService.Object); ImplicitFlowProcessor processor = new ImplicitFlowProcessor(mckFactory.Object); processor.Process(context); Assert.AreEqual(token, context.Token); mckClientService.VerifyAll(); mckFactory.VerifyAll(); mckTokenService.VerifyAll(); }
public void TestProcessingValidContext() { TokenContext context = new TokenContext() { Client = new ClientBase { ClientId = "321", ClientSecret = "secret" }, AuthorizationCode = "123", GrantType = Parameters.GrantTypeValues.AuthorizationCode, RedirectUri = new Uri("http://www.mysites.com/callback"), Scope = new string[] { "create", "delete" } }; IAuthorizationGrant authorizationGrant = new AuthorizationGrantBase { Code = "123" }; Mock <IAuthorizationGrantService> mckAuthService = new Mock <IAuthorizationGrantService>(); mckAuthService.Setup(x => x.FindAuthorizationGrant("123")).Returns(authorizationGrant); mckAuthService.Setup(x => x.ConsumeGrant(It.IsAny <IAuthorizationGrant>())); mckAuthService.Setup(x => x.ValidateGrant(context, authorizationGrant)).Returns(true); Mock <ITokenService> mckTokenService = new Mock <ITokenService>(); mckTokenService.Setup(x => x.IssueAccessToken(authorizationGrant)).Returns(new AccessTokenBase { TokenType = "bearer", RefreshToken = "refresh_token", Token = "token", ExpiresIn = 120 }); Mock <IClientService> mckClientService = new Mock <IClientService>(); mckClientService.Setup(x => x.AuthenticateClient(context)).Returns(true); Mock <IServiceFactory> mckFactory = new Mock <IServiceFactory>(); mckFactory.SetupGet(x => x.ClientService).Returns(mckClientService.Object); mckFactory.SetupGet(x => x.TokenService).Returns(mckTokenService.Object); mckFactory.SetupGet(x => x.AuthorizationGrantService).Returns(mckAuthService.Object); AuthenticationCodeProcessor processor = new AuthenticationCodeProcessor(mckFactory.Object); processor.Process(context); Assert.IsNotNull(context.Token); Assert.AreEqual(120, context.Token.ExpiresIn); Assert.AreEqual("refresh_token", context.Token.RefreshToken); Assert.AreEqual("token", context.Token.Token); mckFactory.VerifyAll(); mckClientService.VerifyAll(); mckTokenService.VerifyAll(); }