Beispiel #1
0
        public async Task ShouldAddNewGame()
        {
            //Arrange command
            var command = new Create.Command()
            {
                Title       = "Fallout 4",
                ReleaseDate = DateTimeHelper.Now
            };
            //Arrange token
            var request = new CreateToken.Command()
            {
                Email      = "*****@*****.**",
                Password   = "******",
                DeviceId   = "SMSNG45543",
                DeviceName = "DEVICE",
                Platform   = Platform.Web
            };
            var token = await _fixture.GetTokenAsync(request);

            _fixture.Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.AccessToken);
            // The endpoint or route of the controller action.
            var httpResponse = await _fixture.Client.PostAsJsonAsync("/api/games", command);

            _fixture.Client.DefaultRequestHeaders.Clear();
            // Must be successful.
            httpResponse.EnsureSuccessStatusCode();

            var addedGame = _fixture.db.Games.FirstOrDefault(u => u.Title == "Fallout 4");

            //Assert
            addedGame.ShouldNotBeNull();
            addedGame.Title.ShouldBe("Fallout 4");
        }
Beispiel #2
0
        public async Task ShouldGetAllUsers()
        {
            var command = new CreateToken.Command()
            {
                Email      = "*****@*****.**",
                Password   = "******",
                DeviceId   = "SMSNG45543",
                DeviceName = "DEVICE",
                Platform   = Platform.Web
            };
            var token = await _fixture.GetTokenAsync(command);

            _fixture.Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.AccessToken);
            // The endpoint or route of the controller action.
            var httpResponse = await _fixture.Client.GetAsync("/api/users");

            _fixture.Client.DefaultRequestHeaders.Clear();
            // Must be successful.
            httpResponse.EnsureSuccessStatusCode();

            // Deserialize and examine results.
            var stringResponse = await httpResponse.Content.ReadAsStringAsync();

            var result = JsonConvert.DeserializeObject <GetAll.Result>(stringResponse);

            //Assert
            result.Users.ShouldNotBeNull();
            result.Users.Count().ShouldBe(3);
            result.Users.ShouldContain(p => p.Email == "*****@*****.**");
            result.Users.ShouldContain(p => p.Email == "*****@*****.**");
            result.Users.ShouldContain(p => p.Email == "*****@*****.**");
        }
Beispiel #3
0
        public async Task ShouldDeleteGame()
        {
            //Arrange token
            var request = new CreateToken.Command()
            {
                Email      = "*****@*****.**",
                Password   = "******",
                DeviceId   = "SMSNG45543",
                DeviceName = "DEVICE",
                Platform   = Platform.Web
            };
            var token = await _fixture.GetTokenAsync(request);

            _fixture.Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.AccessToken);
            // The endpoint or route of the controller action.
            var httpResponse = await _fixture.Client.DeleteAsync("/api/games/1");

            _fixture.Client.DefaultRequestHeaders.Clear();
            // Must be successful.
            httpResponse.EnsureSuccessStatusCode();

            var deletedGame = _fixture.db.Games.FirstOrDefault(g => g.Id == 1);

            //Assert
            deletedGame.ShouldBeNull();
        }
        /// <summary>
        /// gets auth token if provided data from command is valid
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        public async Task <CreateToken.Result> GetTokenAsync(CreateToken.Command command)
        {
            var httpResponse = await Client.PostAsJsonAsync("/api/tokens", command);

            var stringResponse = await httpResponse.Content.ReadAsStringAsync();

            return(JsonConvert.DeserializeObject <CreateToken.Result>(stringResponse));
        }
        public async Task ShouldRefreshToken()
        {
            //Arrange command
            var command = new CreateToken.Command()
            {
                Email      = "*****@*****.**",
                Password   = "******",
                DeviceId   = "SMSNG45543",
                DeviceName = "DEVICE",
                Platform   = Platform.Web
            };
            var token = await _fixture.GetTokenAsync(command);

            //Arrange token request
            var request = new RefreshToken.Command()
            {
                RefreshToken = token.RefreshToken
            };
            // The endpoint or route of the controller action.
            var httpResponse = await _fixture.Client.PostAsJsonAsync("/api/tokens/refresh", request);

            _fixture.Client.DefaultRequestHeaders.Clear();

            // Must be successful.
            httpResponse.EnsureSuccessStatusCode();

            // Deserialize
            var stringResponse = await httpResponse.Content.ReadAsStringAsync();

            var refreshedToken = JsonConvert.DeserializeObject <RefreshToken.Result>(stringResponse);
            var newToken       = new JwtSecurityTokenHandler().ReadToken(refreshedToken.AccessToken) as JwtSecurityToken;

            // get custom claims
            var userIdFromToken = newToken.Claims.First(claim => claim.Type == "sub").Value;
            var loginFromToken  = newToken.Claims.First(claim => claim.Type == "unique_name").Value;

            //get user data
            var user = _fixture.db.Users.FirstOrDefault(u => u.Id == new Guid(userIdFromToken));

            //Assert
            user.Id.ShouldBe(new Guid(userIdFromToken));
            user.Email.ShouldBe(command.Email);
            user.Login.ShouldBe(loginFromToken);
        }
        public async Task ShouldEditGame()
        {
            //Arrange command
            var command = new Edit.Command()
            {
                Title = "Fallout 4"
            };
            //Arrange token
            var request = new CreateToken.Command()
            {
                Email      = "*****@*****.**",
                Password   = "******",
                DeviceId   = "SMSNG45543",
                DeviceName = "DEVICE",
                Platform   = Platform.Web
            };
            var token = await _fixture.GetTokenAsync(request);

            _fixture.Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.AccessToken);
            // The endpoint or route of the controller action.
            var httpResponse = await _fixture.Client.PutAsJsonAsync("/api/games/1", command);

            _fixture.Client.DefaultRequestHeaders.Clear();
            // Must be successful.
            httpResponse.EnsureSuccessStatusCode();

            // get edited game and deserialize
            var stringResponse = await _fixture.Client.GetAsync("/api/games/1").Result.Content.ReadAsStringAsync();

            var editedGame = JsonConvert.DeserializeObject <GetById.Result>(stringResponse);

            // Assert
            editedGame.ShouldNotBeNull();
            editedGame.Id.ShouldBe(1);
            editedGame.Title.ShouldBe("Fallout 4");
        }