Exemplo n.º 1
0
        public void GetAccount_AllParametersOk_AccountExists()
        {
            _mockService.Setup(service => service.GetAccount(It.IsAny <string>()))
            .Returns(new Account {
                AccountID = "12345", Password = "******"
            });

            AccountRequestBody request = new AccountRequestBody()
            {
                AccountId = "12345"
            };

            var actionResult = _controller.GetAccount(request);

            Assert.IsType <ActionResult <AccountResponseBody> >(actionResult);

            ConflictObjectResult conflictResult = (ConflictObjectResult)actionResult.Result;

            Assert.IsType <AccountResponseBody>(conflictResult.Value);

            AccountResponseBody responseBody = (AccountResponseBody)conflictResult.Value;

            Assert.False(responseBody.success);
            Assert.Null(responseBody.password);
            Assert.Equal("Account exists", responseBody.description);
        }
Exemplo n.º 2
0
        public void GetAccount_AllParametersOk_AccountCreated()
        {
            _mockService.Setup(service => service.GetAccount(It.IsAny <string>()))
            .Returns((Account)null);

            _mockService.Setup(service => service.CreateAccount(It.IsAny <string>()))
            .Returns(new Account {
                AccountID = "12345", Password = "******"
            });

            AccountRequestBody request = new AccountRequestBody()
            {
                AccountId = "12345"
            };

            var actionResult = _controller.GetAccount(request);

            Assert.IsType <ActionResult <AccountResponseBody> >(actionResult);

            CreatedResult createdResult = (CreatedResult)actionResult.Result;

            Assert.IsType <AccountResponseBody>(createdResult.Value);

            AccountResponseBody responseBody = (AccountResponseBody)createdResult.Value;

            Assert.True(responseBody.success);
            Assert.Equal("dummy", responseBody.password);
            Assert.Equal("Account created successfully", responseBody.description);
        }
Exemplo n.º 3
0
        public ActionResult <AccountResponseBody> GetAccount([FromBody] AccountRequestBody accountModel)
        {
            AccountResponseBody accountResponse = new AccountResponseBody(); //priprema responsea za popuniti

            //standardni nacin provjere da li su model binding i model validation prosli ok
            if (ModelState.IsValid)
            {
                //trazimo da li account postoji da bi znali koji response poslati
                Account account = _accountService.GetAccount(accountModel.AccountId);

                if (account != null)
                {
                    //account vec postoji, vracamo 409 conflict
                    accountResponse.success     = false;
                    accountResponse.description = "Account exists";
                    return(Conflict(accountResponse));
                }
                else
                {
                    //account ne postoji, kreiramo novi
                    Account newAccount = _accountService.CreateAccount(accountModel.AccountId);
                    //popunjavamo response i vracamo status created:
                    accountResponse.success     = true;
                    accountResponse.description = "Account created successfully";
                    accountResponse.password    = newAccount.Password;
                    return(Created("", accountResponse));
                }
            }
            else
            {
                return(BadRequest(accountResponse));
            }
        }