Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
23 kB
2
Indexable
Never
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using App.BLL;
using App.BLL.DTO;
using App.Contracts.BLL;
using App.Public.DTO.v1.identity.Identity;
using App.Public.DTO.v1;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using Xunit.Abstractions;

namespace Tests.WebApp.IntegrationTests;

public class CurrencyInWalletServiceUnitTests : IClassFixture<CustomWebApplicationFactory<Program>>
{
    private readonly HttpClient _client;
    private readonly CustomWebApplicationFactory<Program> _factory;
    private readonly ITestOutputHelper _testOutputHelper;
    //private readonly Mock<IAppBLL> _bllMock;


    public CurrencyInWalletServiceUnitTests(CustomWebApplicationFactory<Program> factory,
        ITestOutputHelper testOutputHelper)
    {
        _factory = factory;
        _testOutputHelper = testOutputHelper;
        _client = _factory.CreateClient(
            new WebApplicationFactoryClientOptions
            {
                AllowAutoRedirect = false
            }
        );

        /*
        _bllMock = new Mock<IAppBLL>();
        _bllMock.Setup(x => x.Wallet.GetAllAsync(true)).ReturnsAsync(new List<Wallet>()
        {
            new Wallet()
            {
                Id = Guid.Parse("1ba9f0f5-fada-4ab1-8710-1534b0b307d6"),
            },
            new Wallet()
            {
            Id = Guid.Parse("2ba9f0f5-fada-4ab1-8710-1534b0b307d6"),
            }
        });
        
        _bllMock.Setup(x => x.CurrencyInWallet.GetAllAsync(true)).ReturnsAsync(new List<CurrencyInWallet>()
        {
            new CurrencyInWallet()
            {
                Amount = "1",
                WalletId = Guid.Parse("1ba9f0f5-fada-4ab1-8710-1534b0b307d6"),
                CurrencyId = Guid.Parse("2ba9f0f5-fada-4ab1-8710-1534b0b307d6")
            }
        });
        
        _bllMock.Setup(x => x.Currency.GetAllAsync(true)).ReturnsAsync(new List<Currency>()
        {
            new Currency()
            {
                Id = Guid.Parse("2ba9f0f5-fada-4ab1-8710-1534b0b307d1")
            },
            new Currency()
            {
            Id = Guid.Parse("2ba9f0f5-fada-4ab1-8710-1534b0b307d2")
        }
        }
        );*/
        

    }

    [Fact]
    public async Task WalletFromHasFundsToSendAsset_CurrencyNotDouble_Test()
    {
        var loginDto = new Login()
        {
            Email = "test@test.ee",
            Password = "Test_Password!"
        };
        
        var jsonStrLogIn = System.Text.Json.JsonSerializer.Serialize(loginDto);
        var logInData = new StringContent(jsonStrLogIn, Encoding.UTF8, "application/json");

        var responseLogIn = await _client.PostAsync("api/v1/identity/Account/LogIn", logInData);
        responseLogIn.EnsureSuccessStatusCode();
        
        var requestContent = await responseLogIn.Content.ReadAsStringAsync();
        var resultJwt = System.Text.Json.JsonSerializer.Deserialize<JwtResponse>(
            requestContent,
            new JsonSerializerOptions() {PropertyNamingPolicy = JsonNamingPolicy.CamelCase}
        );
        
        var walletDTO = new WalletDTO()
        {
            WalletName = "Test_Wallet"
        };
        
        _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        _client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", resultJwt!.Token);
        
        var jsonStrWallet = System.Text.Json.JsonSerializer.Serialize(walletDTO);
        var walletCreationData = new StringContent(jsonStrWallet, Encoding.UTF8, "application/json");
        var apiresponseCreateWallet = await _client.PostAsync("/api/v1/Wallet/", walletCreationData);
        
        apiresponseCreateWallet.EnsureSuccessStatusCode();
        
        var apiresponseWalletNowCreated = await _client.GetAsync("/api/v1/wallet/");
        
        var requestWalletsCreated = await apiresponseWalletNowCreated.Content.ReadAsStringAsync();
        
        var walletDtoFromResponse = System.Text.Json.JsonSerializer.Deserialize<List<WalletDTO>>(
            requestWalletsCreated,
            new JsonSerializerOptions() {PropertyNamingPolicy = JsonNamingPolicy.CamelCase}
        );
        
        var jsonStrBuy = System.Text.Json.JsonSerializer.Serialize(walletDTO);
        var buyData = new StringContent(jsonStrBuy, Encoding.UTF8, "application/json");
        var apiResponseBuy = await _client.PostAsync($"/api/v1/Buy/{walletDtoFromResponse!.First().Id}/BNB/ASD", buyData);
        
        var requestBuy = await apiResponseBuy.Content.ReadAsStringAsync();
        
        Assert.Contains("Amount is not a number", requestBuy);
    }
    
    [Fact]
    public async Task WalletFromHasFundsToSendAsset_CurrencyNotInDb_Test()
    {
        var loginDto = new Login()
        {
            Email = "test@test.ee",
            Password = "Test_Password!"
        };
        
        var jsonStrLogIn = System.Text.Json.JsonSerializer.Serialize(loginDto);
        var logInData = new StringContent(jsonStrLogIn, Encoding.UTF8, "application/json");

        var responseLogIn = await _client.PostAsync("api/v1/identity/Account/LogIn", logInData);
        responseLogIn.EnsureSuccessStatusCode();
        
        var requestContent = await responseLogIn.Content.ReadAsStringAsync();
        var resultJwt = System.Text.Json.JsonSerializer.Deserialize<JwtResponse>(
            requestContent,
            new JsonSerializerOptions() {PropertyNamingPolicy = JsonNamingPolicy.CamelCase}
        );
        
        var walletDTO = new WalletDTO()
        {
            WalletName = "Test_Wallet"
        };
        
        _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        _client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", resultJwt!.Token);
        
        var jsonStrWallet = System.Text.Json.JsonSerializer.Serialize(walletDTO);
        var walletCreationData = new StringContent(jsonStrWallet, Encoding.UTF8, "application/json");
        var apiresponseCreateWallet = await _client.PostAsync("/api/v1/Wallet/", walletCreationData);
        
        apiresponseCreateWallet.EnsureSuccessStatusCode();
        
        var apiresponseWalletNowCreated = await _client.GetAsync("/api/v1/wallet/");
        
        var requestWalletsCreated = await apiresponseWalletNowCreated.Content.ReadAsStringAsync();
        
        var walletDtoFromResponse = System.Text.Json.JsonSerializer.Deserialize<List<WalletDTO>>(
            requestWalletsCreated,
            new JsonSerializerOptions() {PropertyNamingPolicy = JsonNamingPolicy.CamelCase}
        );
        
        var jsonStrBuy = System.Text.Json.JsonSerializer.Serialize(walletDTO);
        var buyData = new StringContent(jsonStrBuy, Encoding.UTF8, "application/json");
        var apiResponseBuy = await _client.PostAsync($"/api/v1/Buy/{walletDtoFromResponse!.First().Id}/ASD/1", buyData);
        
        var requestBuy = await apiResponseBuy.Content.ReadAsStringAsync();
        
        Assert.Contains("Wallet or currency does not exist", requestBuy);
    }
    
    [Fact]
    public async Task WalletFromHasFundsToSendAsset_WrongGuid_Test()
    {
        var loginDto = new Login()
        {
            Email = "test@test.ee",
            Password = "Test_Password!"
        };
        
        var jsonStrLogIn = System.Text.Json.JsonSerializer.Serialize(loginDto);
        var logInData = new StringContent(jsonStrLogIn, Encoding.UTF8, "application/json");

        var responseLogIn = await _client.PostAsync("api/v1/identity/Account/LogIn", logInData);
        responseLogIn.EnsureSuccessStatusCode();
        
        var requestContent = await responseLogIn.Content.ReadAsStringAsync();
        var resultJwt = System.Text.Json.JsonSerializer.Deserialize<JwtResponse>(
            requestContent,
            new JsonSerializerOptions() {PropertyNamingPolicy = JsonNamingPolicy.CamelCase}
        );
        
        var walletDTO = new WalletDTO()
        {
            WalletName = "Test_Wallet"
        };
        
        _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        _client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", resultJwt!.Token);
        
        var jsonStrWallet = System.Text.Json.JsonSerializer.Serialize(walletDTO);
        var walletCreationData = new StringContent(jsonStrWallet, Encoding.UTF8, "application/json");
        var apiresponseCreateWallet = await _client.PostAsync("/api/v1/Wallet/", walletCreationData);
        
        apiresponseCreateWallet.EnsureSuccessStatusCode();
        
        var apiresponseWalletNowCreated = await _client.GetAsync("/api/v1/wallet/");
        
        var requestWalletsCreated = await apiresponseWalletNowCreated.Content.ReadAsStringAsync();
        
        var walletDtoFromResponse = System.Text.Json.JsonSerializer.Deserialize<List<WalletDTO>>(
            requestWalletsCreated,
            new JsonSerializerOptions() {PropertyNamingPolicy = JsonNamingPolicy.CamelCase}
        );
        
        var jsonStrBuy = System.Text.Json.JsonSerializer.Serialize(walletDTO);
        var buyData = new StringContent(jsonStrBuy, Encoding.UTF8, "application/json");
        var apiResponseBuy = await _client.PostAsync($"/api/v1/Buy/{Guid.NewGuid()}/ASD/1", buyData);
        
        var requestBuy = await apiResponseBuy.Content.ReadAsStringAsync();
        
        Assert.Contains("Wallet or currency does not exist", requestBuy);
    }
    
    [Fact]
    public async Task WalletFromHasFundsToSendAsset_WalletDoesNotHaveEnoughFunds_Test()
    {
        var loginDto = new Login()
        {
            Email = "test@test.ee",
            Password = "Test_Password!"
        };
        
        var jsonStrLogIn = System.Text.Json.JsonSerializer.Serialize(loginDto);
        var logInData = new StringContent(jsonStrLogIn, Encoding.UTF8, "application/json");

        var responseLogIn = await _client.PostAsync("api/v1/identity/Account/LogIn", logInData);
        responseLogIn.EnsureSuccessStatusCode();
        
        var requestContent = await responseLogIn.Content.ReadAsStringAsync();
        var resultJwt = System.Text.Json.JsonSerializer.Deserialize<JwtResponse>(
            requestContent,
            new JsonSerializerOptions() {PropertyNamingPolicy = JsonNamingPolicy.CamelCase}
        );
        
        var walletDTO = new WalletDTO()
        {
            WalletName = "Test_Wallet"
        };
        
        _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        _client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", resultJwt!.Token);
        
        var jsonStrWallet = System.Text.Json.JsonSerializer.Serialize(walletDTO);
        var walletCreationData = new StringContent(jsonStrWallet, Encoding.UTF8, "application/json");
        var apiresponseCreateWallet = await _client.PostAsync("/api/v1/Wallet/", walletCreationData);
        
        apiresponseCreateWallet.EnsureSuccessStatusCode();
        
        var apiresponseWalletNowCreated = await _client.GetAsync("/api/v1/wallet/");
        
        var requestWalletsCreated = await apiresponseWalletNowCreated.Content.ReadAsStringAsync();
        
        var walletDtoFromResponse = System.Text.Json.JsonSerializer.Deserialize<List<WalletDTO>>(
            requestWalletsCreated,
            new JsonSerializerOptions() {PropertyNamingPolicy = JsonNamingPolicy.CamelCase}
        );
        
        var jsonStrBuy = System.Text.Json.JsonSerializer.Serialize(walletDTO);
        var buyData = new StringContent(jsonStrBuy, Encoding.UTF8, "application/json");
        var apiResponseBuy = await _client.PostAsync($"/api/v1/Buy/{walletDtoFromResponse!.First().Id}/BNB/1000", buyData);
        
        var requestBuy = await apiResponseBuy.Content.ReadAsStringAsync();
        
        Assert.Contains("Wallet does not have enough funds to make this transaction", requestBuy);
    }
    
    [Fact]
    public async Task WalletFromHasFundsToSendAsset_NegativeAmountToBeSent_Test()
    {
        var loginDto = new Login()
        {
            Email = "test@test.ee",
            Password = "Test_Password!"
        };
        
        var jsonStrLogIn = System.Text.Json.JsonSerializer.Serialize(loginDto);
        var logInData = new StringContent(jsonStrLogIn, Encoding.UTF8, "application/json");

        var responseLogIn = await _client.PostAsync("api/v1/identity/Account/LogIn", logInData);
        responseLogIn.EnsureSuccessStatusCode();
        
        var requestContent = await responseLogIn.Content.ReadAsStringAsync();
        var resultJwt = System.Text.Json.JsonSerializer.Deserialize<JwtResponse>(
            requestContent,
            new JsonSerializerOptions() {PropertyNamingPolicy = JsonNamingPolicy.CamelCase}
        );
        
        var walletDTO = new WalletDTO()
        {
            WalletName = "Test_Wallet"
        };
        
        _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        _client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", resultJwt!.Token);
        
        var jsonStrWallet = System.Text.Json.JsonSerializer.Serialize(walletDTO);
        var walletCreationData = new StringContent(jsonStrWallet, Encoding.UTF8, "application/json");
        var apiresponseCreateWallet = await _client.PostAsync("/api/v1/Wallet/", walletCreationData);
        
        apiresponseCreateWallet.EnsureSuccessStatusCode();
        
        var apiresponseWalletNowCreated = await _client.GetAsync("/api/v1/wallet/");
        
        var requestWalletsCreated = await apiresponseWalletNowCreated.Content.ReadAsStringAsync();
        
        var walletDtoFromResponse = System.Text.Json.JsonSerializer.Deserialize<List<WalletDTO>>(
            requestWalletsCreated,
            new JsonSerializerOptions() {PropertyNamingPolicy = JsonNamingPolicy.CamelCase}
        );
        
        var jsonStrBuy = System.Text.Json.JsonSerializer.Serialize(walletDTO);
        var buyData = new StringContent(jsonStrBuy, Encoding.UTF8, "application/json");
        var apiResponseBuy = await _client.PostAsync($"/api/v1/Buy/{walletDtoFromResponse!.First().Id}/BNB/-1", buyData);
        
        var requestBuy = await apiResponseBuy.Content.ReadAsStringAsync();
        
        Assert.Contains("Negative amounts can not be Bought", requestBuy);
    }

    [Fact]
    public async Task WalletFromHasFundsToSendAsset_SellNotExistingCurrency_Test()
    {
        var loginDto = new Login()
        {
            Email = "test@test.ee",
            Password = "Test_Password!"
        };
        
        var jsonStrLogIn = System.Text.Json.JsonSerializer.Serialize(loginDto);
        var logInData = new StringContent(jsonStrLogIn, Encoding.UTF8, "application/json");

        var responseLogIn = await _client.PostAsync("api/v1/identity/Account/LogIn", logInData);
        responseLogIn.EnsureSuccessStatusCode();
        
        var requestContent = await responseLogIn.Content.ReadAsStringAsync();
        var resultJwt = System.Text.Json.JsonSerializer.Deserialize<JwtResponse>(
            requestContent,
            new JsonSerializerOptions() {PropertyNamingPolicy = JsonNamingPolicy.CamelCase}
        );
        
        var walletDTO = new WalletDTO()
        {
            WalletName = "Test_Wallet"
        };
        
        _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        _client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", resultJwt!.Token);
        
        var jsonStrWallet = System.Text.Json.JsonSerializer.Serialize(walletDTO);
        var walletCreationData = new StringContent(jsonStrWallet, Encoding.UTF8, "application/json");
        var apiresponseCreateWallet = await _client.PostAsync("/api/v1/Wallet/", walletCreationData);
        
        apiresponseCreateWallet.EnsureSuccessStatusCode();
        
        var apiresponseWalletNowCreated = await _client.GetAsync("/api/v1/wallet/");
        
        var requestWalletsCreated = await apiresponseWalletNowCreated.Content.ReadAsStringAsync();
        
        var walletDtoFromResponse = System.Text.Json.JsonSerializer.Deserialize<List<WalletDTO>>(
            requestWalletsCreated,
            new JsonSerializerOptions() {PropertyNamingPolicy = JsonNamingPolicy.CamelCase}
        );
        
        var jsonStrBuy = System.Text.Json.JsonSerializer.Serialize(walletDTO);
        var buyData = new StringContent(jsonStrBuy, Encoding.UTF8, "application/json");
        var apiResponseBuy = await _client.PostAsync($"/api/v1/Buy/{walletDtoFromResponse!.First().Id}/BNB/1", buyData);
        var apiResponseSell = await _client.PostAsync($"/api/v1/Sell/{walletDtoFromResponse!.First().Id}/ASD/1", buyData);
        
        var requestBuy = await apiResponseSell.Content.ReadAsStringAsync();
        
        Assert.Contains("Wallet or currency does not exist", requestBuy);
    }
    
    [Fact]
    public async Task WalletFromHasFundsToSendAsset_SellTooMuchOfCurrency_Test()
    {
        var loginDto = new Login()
        {
            Email = "test@test.ee",
            Password = "Test_Password!"
        };
        
        var jsonStrLogIn = System.Text.Json.JsonSerializer.Serialize(loginDto);
        var logInData = new StringContent(jsonStrLogIn, Encoding.UTF8, "application/json");

        var responseLogIn = await _client.PostAsync("api/v1/identity/Account/LogIn", logInData);
        responseLogIn.EnsureSuccessStatusCode();
        
        var requestContent = await responseLogIn.Content.ReadAsStringAsync();
        var resultJwt = System.Text.Json.JsonSerializer.Deserialize<JwtResponse>(
            requestContent,
            new JsonSerializerOptions() {PropertyNamingPolicy = JsonNamingPolicy.CamelCase}
        );
        
        var walletDTO = new WalletDTO()
        {
            WalletName = "Test_Wallet"
        };
        
        _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        _client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", resultJwt!.Token);
        
        var jsonStrWallet = System.Text.Json.JsonSerializer.Serialize(walletDTO);
        var walletCreationData = new StringContent(jsonStrWallet, Encoding.UTF8, "application/json");
        var apiresponseCreateWallet = await _client.PostAsync("/api/v1/Wallet/", walletCreationData);
        
        apiresponseCreateWallet.EnsureSuccessStatusCode();
        
        var apiresponseWalletNowCreated = await _client.GetAsync("/api/v1/wallet/");
        
        var requestWalletsCreated = await apiresponseWalletNowCreated.Content.ReadAsStringAsync();
        
        var walletDtoFromResponse = System.Text.Json.JsonSerializer.Deserialize<List<WalletDTO>>(
            requestWalletsCreated,
            new JsonSerializerOptions() {PropertyNamingPolicy = JsonNamingPolicy.CamelCase}
        );
        
        var jsonStrBuy = System.Text.Json.JsonSerializer.Serialize(walletDTO);
        var buyData = new StringContent(jsonStrBuy, Encoding.UTF8, "application/json");
        var apiResponseBuy = await _client.PostAsync($"/api/v1/Buy/{walletDtoFromResponse!.First().Id}/BNB/1", buyData);
        var apiResponseSell = await _client.PostAsync($"/api/v1/Sell/{walletDtoFromResponse!.First().Id}/BNB/59", buyData);
        
        var requestBuy = await apiResponseSell.Content.ReadAsStringAsync();
        
        Assert.Contains("Wallet does not have enough funds to make this transaction", requestBuy);
    }
    
    [Fact]
    public async Task WalletFromHasFundsToSendAsset_SellNegavtiveAmountOfCurrency()
    {
        var loginDto = new Login()
        {
            Email = "test@test.ee",
            Password = "Test_Password!"
        };
        
        var jsonStrLogIn = System.Text.Json.JsonSerializer.Serialize(loginDto);
        var logInData = new StringContent(jsonStrLogIn, Encoding.UTF8, "application/json");

        var responseLogIn = await _client.PostAsync("api/v1/identity/Account/LogIn", logInData);
        responseLogIn.EnsureSuccessStatusCode();
        
        var requestContent = await responseLogIn.Content.ReadAsStringAsync();
        var resultJwt = System.Text.Json.JsonSerializer.Deserialize<JwtResponse>(
            requestContent,
            new JsonSerializerOptions() {PropertyNamingPolicy = JsonNamingPolicy.CamelCase}
        );
        
        var walletDTO = new WalletDTO()
        {
            WalletName = "Test_Wallet"
        };
        
        _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        _client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", resultJwt!.Token);
        
        var jsonStrWallet = System.Text.Json.JsonSerializer.Serialize(walletDTO);
        var walletCreationData = new StringContent(jsonStrWallet, Encoding.UTF8, "application/json");
        var apiresponseCreateWallet = await _client.PostAsync("/api/v1/Wallet/", walletCreationData);
        
        apiresponseCreateWallet.EnsureSuccessStatusCode();
        
        var apiresponseWalletNowCreated = await _client.GetAsync("/api/v1/wallet/");
        
        var requestWalletsCreated = await apiresponseWalletNowCreated.Content.ReadAsStringAsync();
        
        var walletDtoFromResponse = System.Text.Json.JsonSerializer.Deserialize<List<WalletDTO>>(
            requestWalletsCreated,
            new JsonSerializerOptions() {PropertyNamingPolicy = JsonNamingPolicy.CamelCase}
        );
        
        var jsonStrBuy = System.Text.Json.JsonSerializer.Serialize(walletDTO);
        var buyData = new StringContent(jsonStrBuy, Encoding.UTF8, "application/json");
        var apiResponseBuy = await _client.PostAsync($"/api/v1/Buy/{walletDtoFromResponse!.First().Id}/BNB/1", buyData);
        var apiResponseSell = await _client.PostAsync($"/api/v1/Sell/{walletDtoFromResponse!.First().Id}/BNB/-1", buyData);
        
        var requestBuy = await apiResponseSell.Content.ReadAsStringAsync();
        
        Assert.Contains("Negative amounts can not be sold", requestBuy);
    }
}