Beispiel #1
0
        public async Task Should_remove_token_per_device()
        {
            // given
            var    now   = DateTime.UtcNow;
            string email = "*****@*****.**";

            string firstRefreshToken    = "F5 F5";
            string firstDeviceIpAddress = "8.8.8.8";

            string secondRefreshToken    = "F6 F6";
            string secondDeviceIpAddress = "8.8.8.9";

            UserRefreshTokenRepository repository = CreateRepository();

            // when
            await repository.AddRefreshToken(email, firstRefreshToken, firstDeviceIpAddress);

            await repository.AddRefreshToken(email, secondRefreshToken, secondDeviceIpAddress);

            await repository.DeleteRefreshTokens(email, firstDeviceIpAddress);

            // then
            UserRefreshToken token1 = await repository.GetRefreshToken(email, firstDeviceIpAddress);

            UserRefreshToken token2 = await repository.GetRefreshToken(email, secondDeviceIpAddress);

            token1.ShouldBeNull();
            token2.ShouldNotBeNull();
        }
Beispiel #2
0
        public async Task Should_add_and_get_token_per_device()
        {
            // given
            var    now   = DateTime.UtcNow;
            string email = "*****@*****.**";

            string firstRefreshToken    = "F5 F5";
            string firstDeviceIpAddress = "8.8.8.8";

            string secondRefreshToken    = "F6 F6";
            string secondDeviceIpAddress = "8.8.8.9";

            UserRefreshTokenRepository repository = CreateRepository();

            // when
            await repository.AddRefreshToken(email, firstRefreshToken, firstDeviceIpAddress);

            await repository.AddRefreshToken(email, secondRefreshToken, secondDeviceIpAddress);

            UserRefreshToken token = await repository.GetRefreshToken(email, firstDeviceIpAddress);

            // then
            token.ShouldNotBeNull();
            token.Email.ShouldBe(email);
            token.RefreshToken.ShouldBe(firstRefreshToken);
            token.CreationDate.ShouldBeGreaterThanOrEqualTo(now);
            token.IpAddress.ShouldBe(firstDeviceIpAddress);
        }
Beispiel #3
0
        public async Task Delete_ReturnsDeletedToken()
        {
            string usersJson = File.ReadAllText(TestConfiguration.MockDataFolderPath + @"Users.json");
            var    user      = JsonConvert.DeserializeObject <List <User> >(usersJson).First(u => u.Id == TESTING_USER_ID);

            const int    refreshTokenId = 2;
            const string token          = "qqqqqqqqqqqqqqqqqqqqqqqwwwwwwwee";
            DateTime     expirationDate = DateTime.Now.AddMinutes(5);

            var newRefreshToken = new UserRefreshToken
            {
                Id          = refreshTokenId,
                User        = user,
                Token       = token,
                ExpiredDate = expirationDate
            };

            UserRefreshTokenRepository.Setup(r => r.GetByKey(newRefreshToken.Id)).Returns(newRefreshToken);

            UserRefreshTokenRepository.Setup(r => r.Delete(newRefreshToken.Id)).ReturnsAsync(newRefreshToken);


            var resultToken = await UserRefreshTokenService.Delete(newRefreshToken.Id);


            Assert.NotNull(resultToken);
            Assert.NotNull(resultToken.User);
            Assert.Equal(TESTING_USER_ID, resultToken.User.Id);
            Assert.Equal(token, resultToken.Token);
            Assert.Equal(expirationDate, resultToken.ExpiredDate);
        }
        public async Task Should_remove_tokens_for_user_email()
        {
            // given
            string email1          = "*****@*****.**";
            string tokenForUser1   = "token stuart 1";
            string tokenForUser1_b = "token stuart 2";

            string email2        = "*****@*****.**";
            string tokenForUser2 = "token for bob";

            UserRefreshTokenRepository repository = CreateRepository();
            await repository.AddRefreshToken("jwt1", tokenForUser1, email1, "ip1");

            await repository.AddRefreshToken("jwt2", tokenForUser1_b, email1, "ip2");

            await repository.AddRefreshToken("jwt3", tokenForUser2, email2, "ip3");

            // when
            await repository.DeleteRefreshTokens(email1);

            // then
            UserRefreshToken deletedToken1 = await repository.GetRefreshToken(tokenForUser1);

            UserRefreshToken deletedToken2 = await repository.GetRefreshToken(tokenForUser1_b);

            UserRefreshToken activeToken = await repository.GetRefreshToken(tokenForUser2);

            deletedToken1.ShouldBeNull();
            deletedToken2.ShouldBeNull();

            activeToken.ShouldNotBeNull();
        }
        public async Task Should_remove_token()
        {
            // given
            string email1       = "*****@*****.**";
            string refreshToken = "token stuart 1";

            UserRefreshTokenRepository repository = CreateRepository();
            await repository.AddRefreshToken("jwt1", refreshToken, email1, "ip1");

            // when
            await repository.DeleteRefreshToken(refreshToken);

            // then
            UserRefreshToken deletedToken = await repository.GetRefreshToken(refreshToken);

            deletedToken.ShouldBeNull();
        }
        public async Task Should_add_token_and_fill_properties()
        {
            // given
            var    now          = DateTime.UtcNow;
            string jwtToken     = "jwt";
            string email        = "*****@*****.**";
            string refreshToken = "token stuart 1";
            string ip           = "ip1";

            UserRefreshTokenRepository repository = CreateRepository();

            // when
            await repository.AddRefreshToken(jwtToken, refreshToken, email, ip);

            // then
            UserRefreshToken token = await repository.GetRefreshToken(refreshToken);

            token.ShouldNotBeNull();
            token.JwtToken.ShouldBe(jwtToken);
            token.RefreshToken.ShouldBe(refreshToken);
            token.CreationDate.ShouldBeGreaterThanOrEqualTo(now);
            token.IpAddress.ShouldBe(ip);
            token.Email.ShouldBe(email);
        }