Exemplo n.º 1
0
        public Task <HttpResponseMessage> AddMoney(int id, [FromBody] AddMoneyRequest addMoneyRequest)
        {
            string token = Request.Headers.GetValues("OnlineCasino-Token").FirstOrDefault();

            if (!loginRepository.HasUserAndToken(id, token))
            {
                throw new ForbiddenException();
            }

            decimal money = addMoneyRequest.AddMoney;

            var user = userRepository.AddMoney(id, money);

            var response = new
            {
                NewBalance = user.Money
            };

            return(Task.FromResult(Request.CreateResponse(HttpStatusCode.OK, response)));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Store([FromBody] AddMoneyRequest request)
        {
            try
            {
                var currency = await _currencyService.GetCurrencyAsync(request.CurrencyId);

                var money = new Money()
                {
                    Currency = currency, Amount = request.Amount
                };
                var user = await _userService.GetUserAsync(request.Username);

                user.Balance.AddMoney(money);
                var result = await _userService.UpdateUserBalance(user);

                return(Ok(result.Balance.GetAllMonies()));
            }
            catch (Exception e)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, e.Message));
            }
        }
Exemplo n.º 3
0
        public async Task AddMoney_ValidData_ReturnsNewBalanceAndOKStatusCode()
        {
            var user = new UserDto()
            {
                Id    = 1,
                Money = 1234
            };

            //Arrange
            Mock <IUserRepository>  mockUserRepository  = new Mock <IUserRepository>();
            Mock <ILoginRepository> mockLoginRepository = new Mock <ILoginRepository>();

            mockLoginRepository.Setup(l => l.HasUserAndToken(user.Id, It.IsAny <string>())).Returns(true);
            mockUserRepository.Setup(u => u.AddMoney(user.Id, user.Money)).Returns(user);

            var controller     = new UserController(mockUserRepository.Object, mockLoginRepository.Object);
            var requestMessage = new HttpRequestMessage();

            requestMessage.Headers.Add("OnlineCasino-Token", "token");
            controller.Request       = requestMessage;
            controller.Configuration = new HttpConfiguration();

            //Act
            var addMoneyRequest = new AddMoneyRequest()
            {
                AddMoney = user.Money
            };
            var response = await controller.AddMoney(user.Id, addMoneyRequest);

            //Assert
            string cnt = await response.Content.ReadAsStringAsync();

            dynamic respData = JsonConvert.DeserializeObject(cnt);

            Assert.IsTrue(respData.NewBalance == addMoneyRequest.AddMoney);

            Assert.IsTrue(response.StatusCode == HttpStatusCode.OK);
        }
Exemplo n.º 4
0
        public async Task GetUserBalanceAndAddMoneyAndCheckIfBalanceIsUpdated()
        {
            string userIdString = TestHelper.TestUser.Id.ToString();

            // Get user balance

            // Arrange
            var request = TestHelper.GenerateRequestMessage(url: "http://localhost/api/users/" + userIdString + "/wallet",
                                                            method: HttpMethod.Get,
                                                            token: TestHelper.TestLogin.Token);

            // Act
            var response = await TestHelper.Client.SendAsync(request);

            // Assert
            Assert.IsTrue(response.StatusCode == HttpStatusCode.OK);
            string content = await response.Content.ReadAsStringAsync();

            var jsonResponse = JsonConvert.DeserializeObject <Dictionary <string, string> >(content);

            var initialBalance = decimal.Parse(jsonResponse["balance"]);

            // Add money to user account

            // Arrange 2
            var addMoneyRequest = new AddMoneyRequest()
            {
                AddMoney = 5000
            };

            var json = JsonConvert.SerializeObject(addMoneyRequest);

            request = TestHelper.GenerateRequestMessage(url: "http://localhost/api/users/" + userIdString + "/wallet",
                                                        method: HttpMethod.Put,
                                                        token: TestHelper.TestLogin.Token,
                                                        json: json);

            // Act 2
            response = await TestHelper.Client.SendAsync(request);

            // Assert 2
            Assert.IsTrue(response.StatusCode == HttpStatusCode.OK);
            content = await response.Content.ReadAsStringAsync();

            jsonResponse = JsonConvert.DeserializeObject <Dictionary <string, string> >(content);

            var newBalance = decimal.Parse(jsonResponse["newBalance"]);

            Assert.IsTrue(newBalance == initialBalance + addMoneyRequest.AddMoney);

            // Get balance data again to see if updated

            // Arrange 3
            request = TestHelper.GenerateRequestMessage(url: "http://localhost/api/users/" + userIdString + "/wallet",
                                                        method: HttpMethod.Get,
                                                        token: TestHelper.TestLogin.Token);

            // Act 3
            response = await TestHelper.Client.SendAsync(request);

            // Assert 3
            Assert.IsTrue(response.StatusCode == HttpStatusCode.OK);
            content = await response.Content.ReadAsStringAsync();

            jsonResponse = JsonConvert.DeserializeObject <Dictionary <string, string> >(content);

            var balance = decimal.Parse(jsonResponse["balance"]);

            Assert.IsTrue(balance == newBalance);

            TestHelper.TestUser.Money = balance;
        }