public void CreateAccountNumberTest() { Assert.IsTrue(AccountUtils.ValidateAccountNumber( AccountUtils.CreateAccountNumber("00109562", 15))); Assert.IsTrue(AccountUtils.ValidateAccountNumber( AccountUtils.CreateAccountNumber("00109562", 1))); Assert.IsTrue(AccountUtils.ValidateAccountNumber( AccountUtils.CreateAccountNumber("00109562", 150000))); Assert.IsTrue(AccountUtils.ValidateAccountNumber( AccountUtils.CreateAccountNumber("00109562", 00150000))); Assert.IsTrue(AccountUtils.ValidateAccountNumber("02001095620000000000000001")); }
private bool CheckTransferParameters() { var proxy = new Proxy.Proxy(); if (!AccountUtils.ValidateAccountNumber(AccountFromTextBox.Text) || proxy.GetUserAccounts(MainWindow.Credentials).FirstOrDefault(x => x.BankAccountNumber == AccountFromTextBox.Text) == null) { ClientUtils.ShowMessage(new ErrorMessage(_resourceWrapper.InvalidBankAccountFrom)); return(false); } if (!AccountUtils.ValidateAccountNumber(AccountToTextBox.Text)) { ClientUtils.ShowMessage(new ErrorMessage(_resourceWrapper.InvalidBankAccountTo)); return(false); } if (AccountFromTextBox.Text == AccountToTextBox.Text) { ClientUtils.ShowMessage(new ErrorMessage(_resourceWrapper.SameFromToBankAccounts)); return(false); } if (TitleTextBox.Text == "") { ClientUtils.ShowMessage(new ErrorMessage(_resourceWrapper.NoTitleError)); return(false); } decimal amountDec; if (decimal.TryParse(AmountTextBox.Text.Replace(".", ","), out amountDec) && amountDec > 0) { return(true); } ClientUtils.ShowMessage(new ErrorMessage(_resourceWrapper.WrongAmount)); return(false); }
/// <summary> /// One and only rest service enpoint hanlding external transfer. /// </summary> /// <param name="stream">Gets json from request body. Json deserialize to <see cref="Transfer"/></param> /// <param name="bankAccountNumberTo">26-digit destination bank account number</param> /// <returns></returns> public Stream RecieveTransfer(Stream stream, string bankAccountNumberTo) { var sr = new StreamReader(stream); var res = sr.ReadToEnd(); var ctx = WebOperationContext.Current; if (ctx == null) { return(AccountUtils.CreateJsonErrorResponse("Bład wewnętrzny")); } if (!ctx.IncomingRequest.Headers[HttpRequestHeader.ContentType].Contains("application/json")) { ctx.OutgoingResponse.StatusCode = HttpStatusCode.BadRequest; return(AccountUtils.CreateJsonErrorResponse("Zły nagłówek: " + HttpRequestHeader.ContentType)); } var transfer = JsonConvert.DeserializeObject <Transfer>(res); // Getting WWW-Authenticate header from POST request. eqample "Basic 23sd1" // Substring cuts "Basic " var credentials = ctx.IncomingRequest.Headers[HttpRequestHeader.Authorization].Substring(6); var truth = AccountUtils.Base64Encode("admin:admin"); // Checks credentials if (truth != credentials) { ctx.OutgoingResponse.StatusCode = HttpStatusCode.Forbidden; return(AccountUtils.CreateJsonErrorResponse("Błąd uwierzytelniania")); } // Checks destination bank account number in terms of checksum var isValidTo = AccountUtils.ValidateAccountNumber(bankAccountNumberTo); if (!isValidTo) { ctx.OutgoingResponse.StatusCode = HttpStatusCode.NotFound; return(AccountUtils.CreateJsonErrorResponse($"Niepoprawne konto: {bankAccountNumberTo}")); } // Checks if destination and source bank account numbers are equal if (bankAccountNumberTo == transfer.From) { ctx.OutgoingResponse.StatusCode = HttpStatusCode.BadRequest; return(AccountUtils.CreateJsonErrorResponse("Konto docelowe musi być różne od konta źródłowego !")); } var collection = MongoRepository.GetCollection <User>(); var filterTo = Builders <User> .Filter.Where( x => x.Accounts.Any(a => a.BankAccountNumber == bankAccountNumberTo)); var accountTo = collection.Find(filterTo) .FirstOrDefault().Accounts.FirstOrDefault(a => a.BankAccountNumber == bankAccountNumberTo); // If there is no destination account in database then return 404 code if (accountTo == null) { ctx.OutgoingResponse.StatusCode = HttpStatusCode.NotFound; return(AccountUtils.CreateJsonErrorResponse($"Nie znaleziono konta: {bankAccountNumberTo}")); } var newAmountTo = accountTo.Amount + (transfer.Amount / 100); var updateTo = Builders <User> .Update.Set(x => x.Accounts[-1].Amount, newAmountTo); var resultTo = collection.UpdateOne(filterTo, updateTo); if (resultTo.IsAcknowledged) { ctx.OutgoingResponse.StatusCode = HttpStatusCode.Created; return(null); } ctx.OutgoingResponse.StatusCode = HttpStatusCode.InternalServerError; return(AccountUtils.CreateJsonErrorResponse("Bład aktualizacji bazy danych")); }