Exemplo n.º 1
0
        public async Task TestGenerateBearer()
        {
            // arange
            List <IdentityRole <string> > claims = new List <IdentityRole <string> >();

            claims.Add(MockData.StandardUserRoles);
            // request
            var request = new Mock <HttpRequest> ();

            request.Setup(r => r.Headers["Authorization"])
            .Returns("basic [email protected]");
            // context
            var context = new Mock <UsersRepository>();

            context.Setup(c => c.GetUserByNameAsync("*****@*****.**"))
            .Returns <User>(d => MockData.StandardUser);
            context.Setup(c => c.GetRolesById(MockData.StandardUser.Id))
            .Returns <Task <List <IdentityRole <string> > > >(d => Task.FromResult(claims));
            // bearerRepo
            var repo = new Mock <BearerHistoryRepository>();

            repo.Setup(b => b.CreateBearerHistory(MockData.TokenStringStandardUser, MockData.StandardUser))
            .Returns(Task.FromResult(MockData.BearerHistoryEntryStandardUser));
            // service to assert
            var bearerService = new BearerTokenService(context.Object, repo.Object);

            // act
            var result = await bearerService.GenerateBearerToken(request.Object);

            // assert
            Assert.NotEqual("wrong request", result);
            Assert.NotEqual("not valid user", result);
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Token()
        {
            //todo: simplify constructor
            BearerTokenService generator = new BearerTokenService(db, BearerRepo);
            var token = await generator.GenerateBearerToken(Request);

            if (token != "not valid user" || token != "wrong request")
            {
                return(Ok(token));
            }
            return(BadRequest(token));
        }