UserContext fixes, UserDal tests, create TransferDestination
This commit is contained in:
parent
7096c90637
commit
534040b6d7
|
|
@ -34,5 +34,15 @@ namespace TIAM.Database.Test
|
|||
var isConnected = ctx.Database.CanConnect();
|
||||
Assert.IsTrue(isConnected);
|
||||
}
|
||||
|
||||
|
||||
[TestMethod]
|
||||
public void UserDalTest()
|
||||
{
|
||||
using var ctx = new UserDbContext();
|
||||
|
||||
var isConnected = ctx.Database.CanConnect();
|
||||
Assert.IsTrue(isConnected);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
|
|
@ -11,9 +11,11 @@
|
|||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.13" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.13" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="7.0.13" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.13" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.1" />
|
||||
<PackageReference Include="Moq" Version="4.20.69" />
|
||||
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
|
||||
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
|
||||
<PackageReference Include="coverlet.collector" Version="3.2.0" />
|
||||
|
|
|
|||
|
|
@ -0,0 +1,130 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
using TIAM.Database.DataLayers.Users;
|
||||
using TIAM.Database.DbContexts;
|
||||
using TIAM.Entities.Users;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Microsoft.EntityFrameworkCore.ChangeTracking;
|
||||
using System.Net.Sockets;
|
||||
|
||||
namespace TIAM.Database.Tests.DataLayers.Users
|
||||
{
|
||||
[TestClass]
|
||||
public class UserDalTests
|
||||
{
|
||||
private Mock<UserDbContext> _mockContext;
|
||||
private UserDal _userDal;
|
||||
|
||||
[TestInitialize]
|
||||
public void TestInitialize()
|
||||
{
|
||||
var options = new DbContextOptionsBuilder<UserDbContext>()
|
||||
.UseInMemoryDatabase(databaseName: "UserDatabase")
|
||||
.Options;
|
||||
|
||||
_mockContext = new Mock<UserDbContext>(options);
|
||||
|
||||
var mockSet = new Mock<DbSet<User>>();
|
||||
_mockContext.Setup(c => c.Users).Returns(mockSet.Object);
|
||||
|
||||
_userDal = new UserDal(_mockContext.Object);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task GetUserByEmailAsync_ReturnsUser_WhenUserExists()
|
||||
{
|
||||
// Arrange
|
||||
var email = "test@tiam.hu";
|
||||
var user = new User { Email = email };
|
||||
var users = new[] { user }.AsQueryable();
|
||||
|
||||
var mockSet = new Mock<DbSet<User>>();
|
||||
mockSet.As<IQueryable<User>>().Setup(m => m.Provider).Returns(users.Provider);
|
||||
mockSet.As<IQueryable<User>>().Setup(m => m.Expression).Returns(users.Expression);
|
||||
mockSet.As<IQueryable<User>>().Setup(m => m.ElementType).Returns(users.ElementType);
|
||||
mockSet.As<IQueryable<User>>().Setup(m => m.GetEnumerator()).Returns(users.GetEnumerator());
|
||||
|
||||
_mockContext.Setup(c => c.Users).Returns(mockSet.Object);
|
||||
|
||||
// Act
|
||||
var result = await _userDal.GetUserByEmailAsync(email);
|
||||
|
||||
// Assert
|
||||
Assert.IsNotNull(result);
|
||||
Assert.AreEqual(email, result.Email);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task GetUserByEmailAsync_ReturnsNull_WhenUserDoesNotExist()
|
||||
{
|
||||
// Arrange
|
||||
var email = "test@test.hu";
|
||||
var users = new User[0].AsQueryable();
|
||||
|
||||
var mockSet = new Mock<DbSet<User>>();
|
||||
mockSet.As<IQueryable<User>>().Setup(m => m.Provider).Returns(users.Provider);
|
||||
mockSet.As<IQueryable<User>>().Setup(m => m.Expression).Returns(users.Expression);
|
||||
mockSet.As<IQueryable<User>>().Setup(m => m.ElementType).Returns(users.ElementType);
|
||||
mockSet.As<IQueryable<User>>().Setup(m => m.GetEnumerator()).Returns(users.GetEnumerator());
|
||||
|
||||
_mockContext.Setup(c => c.Users).Returns(mockSet.Object);
|
||||
|
||||
// Act
|
||||
var result = await _userDal.GetUserByEmailAsync(email);
|
||||
|
||||
// Assert
|
||||
Assert.IsNull(result);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task CreateUserAsync_ShouldReturnTrue_WhenUserIsCreated()
|
||||
{
|
||||
// Arrange
|
||||
var user = new User
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Email = "test@test.com",
|
||||
PhoneNumber = "+1234567890",
|
||||
Password = "password"
|
||||
};
|
||||
|
||||
var mockSet = new Mock<DbSet<User>>();
|
||||
_mockContext.Setup(x => x.Users).Returns(mockSet.Object);
|
||||
_mockContext.Setup(x => x.Users.Add(user)).Returns(() => null);
|
||||
|
||||
_mockContext.Setup(x => x.SaveChangesAsync(It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(1)
|
||||
.Verifiable();
|
||||
|
||||
// Act
|
||||
var result = await _userDal.CreateUserAsync(user);
|
||||
|
||||
// Assert
|
||||
Assert.IsTrue(result);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task UpdateUserAsync_ShouldUpdateUser()
|
||||
{
|
||||
// Arrange
|
||||
var user = new User { Email = "test@test.com", PhoneNumber = "+1234567890", Password = "password" };
|
||||
_mockContext.Object.Users.Add(user);
|
||||
await _mockContext.Object.SaveChangesAsync();
|
||||
|
||||
// Act
|
||||
user.Email = "updated@test.com";
|
||||
var result = await _userDal.UpdateUserAsync(user);
|
||||
|
||||
// Assert
|
||||
Assert.IsTrue(result);
|
||||
var updatedUser = _mockContext.Object.Users.Single(u => u.Email == user.Email);
|
||||
Assert.AreEqual("updated@test.com", updatedUser.Email);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -16,12 +16,16 @@ namespace TIAM.Database.DataLayers.Users
|
|||
{
|
||||
}
|
||||
|
||||
public UserDal(UserDbContext _object)
|
||||
{
|
||||
}
|
||||
|
||||
public Task<List<User>> GetUsersAsync()
|
||||
{
|
||||
return Ctx.Users.ToListAsync();
|
||||
}
|
||||
|
||||
public Task<User?> GetUserByEmailAsync(string email)
|
||||
public virtual Task<User?> GetUserByEmailAsync(string email)
|
||||
{
|
||||
Console.WriteLine($"Getting user from db {email}");
|
||||
var emailLower = email.ToLower();
|
||||
|
|
|
|||
|
|
@ -12,13 +12,18 @@ namespace TIAM.Database.DbContexts
|
|||
{
|
||||
public class UserDbContext : TiamDbContextBase
|
||||
{
|
||||
public DbSet<User> Users { get; set; }
|
||||
public virtual DbSet<User> Users { get; set; }
|
||||
|
||||
public UserDbContext() //: this(string.Empty)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public UserDbContext(DbContextOptions<UserDbContext> options) //: this(string.Empty)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public UserDbContext(string name) : base(name)
|
||||
{
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,22 +9,18 @@ namespace TIAM.Entities.TransferDestinations
|
|||
public class TransferDestination : LocationBase, ITimeStampInfo
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public double Longitude { get; set; }
|
||||
public double Latitude { get; set; }
|
||||
public string? Address { get; set; }
|
||||
public string? Name { get; set; }
|
||||
|
||||
public string? Description { get; set; }
|
||||
public double Latitude { get; set; }
|
||||
public double Longitude { get; set; }
|
||||
public string? Address { get; set; }
|
||||
public DateTime Created { get; set; }
|
||||
public DateTime Modified { get; set; }
|
||||
|
||||
public TransferDestination() { }
|
||||
|
||||
public TransferDestination(double longitude, double latitude, string address) : this(Guid.NewGuid(), longitude, latitude, address) { }
|
||||
|
||||
public TransferDestination(Guid id, double longitude, double latitude, string address) : base(Guid.NewGuid(), longitude, latitude, address) { }
|
||||
|
||||
|
||||
public TransferDestination(Guid id, string name, double longitude, double latitude, string address) : base(id, longitude, latitude, address) { }
|
||||
public TransferDestination(double latitude, double longitude, string address) : this(Guid.NewGuid(), latitude, longitude, address) { }
|
||||
public TransferDestination(Guid id, double latitude, double longitude, string address) : base(Guid.NewGuid(), latitude, longitude, address) { }
|
||||
public TransferDestination(Guid id, string name, double latitude, double longitude, string address) : base(id, latitude,longitude, address) { }
|
||||
public TransferDestination(Guid id, string name, string description, double latitude, double longitude, string address) : base(id, latitude,longitude, address) { }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
using TIAM.Entities.TransferDestinations;
|
||||
using TIAMWebApp.Shared.Application.Interfaces;
|
||||
using TIAMWebApp.Shared.Application.Models;
|
||||
using TIAMWebApp.Shared.Application.Models.ClientSide;
|
||||
|
||||
namespace TIAMMobileApp.Services
|
||||
{
|
||||
|
|
@ -17,14 +18,16 @@ namespace TIAMMobileApp.Services
|
|||
|
||||
/// <summary>
|
||||
/// calls the TransferDataAPI to get the list of destinations
|
||||
public Task<TransferDestination[]?> GetDestinationsAsync()
|
||||
public async Task<TransferDestination[]?> GetDestinationsAsync()
|
||||
{
|
||||
return http.GetFromJsonAsync<TransferDestination[]>("TransferDataAPI");
|
||||
var url = $"{Setting.BaseUrl}/{APIUrls.GetTransferDestinations}";
|
||||
return await http.GetFromJsonAsync<TransferDestination[]>(url);
|
||||
}
|
||||
|
||||
public Task<TransferDestination?> GetDestinationAsync(string destinationId)
|
||||
public async Task<TransferDestination?> GetTransferDestinationbyCoordinatesAsync(string destinationId)
|
||||
{
|
||||
return http.GetFromJsonAsync<TransferDestination?>($"TransferDataAPI/{destinationId}");
|
||||
var url = $"{Setting.BaseUrl}/{APIUrls.GetTransferDestinationByCoordinates}";
|
||||
return await http.GetFromJsonAsync<TransferDestination>(url);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,21 +2,40 @@
|
|||
using TIAM.Entities.TransferDestinations;
|
||||
using TIAMWebApp.Shared.Application.Interfaces;
|
||||
using TIAMWebApp.Shared.Application.Models;
|
||||
using TIAMWebApp.Shared.Application.Models.ClientSide;
|
||||
using TIAMWebApp.Shared.Application.Utility;
|
||||
|
||||
namespace TIAMWebApp.Client.Services
|
||||
{
|
||||
public class TransferDataService : ITransferDataService
|
||||
{
|
||||
private readonly HttpClient http;
|
||||
private readonly LogToBrowserConsole logToBrowserConsole;
|
||||
|
||||
public TransferDataService(HttpClient http)
|
||||
public TransferDataService(HttpClient http, LogToBrowserConsole logToBrowserConsole)
|
||||
{
|
||||
this.http = http;
|
||||
this.logToBrowserConsole = logToBrowserConsole;
|
||||
}
|
||||
|
||||
public Task<TransferDestination[]?> GetDestinationsAsync()
|
||||
public async Task<TransferDestination[]?> GetDestinationsAsync()
|
||||
{
|
||||
return http.GetFromJsonAsync<TransferDestination[]>("TransferDataAPI");
|
||||
|
||||
var url = $"{Setting.BaseUrl}/{APIUrls.GetTransferDestinations}";
|
||||
logToBrowserConsole.LogToBC(url);
|
||||
return await http.GetFromJsonAsync<TransferDestination[]>(url);
|
||||
}
|
||||
|
||||
public async Task<TransferDestination?> GetTransferDestinationbyCoordinatesAsync(string destinationId)
|
||||
{
|
||||
var url = $"{Setting.BaseUrl}/{APIUrls.GetTransferDestinationByCoordinates}";
|
||||
return await http.GetFromJsonAsync<TransferDestination>(url);
|
||||
}
|
||||
|
||||
public async Task<TransferDestination?> GetTransferDestinationbyAddressAsync(string destinationId)
|
||||
{
|
||||
var url = $"{Setting.BaseUrl}/{APIUrls.GetTransferDestinationByAddress}";
|
||||
return await http.GetFromJsonAsync<TransferDestination>(url);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -85,11 +85,13 @@ namespace TIAMWebApp.Server.Controllers
|
|||
if (transferDestination != null)
|
||||
{
|
||||
var Id = Guid.NewGuid();
|
||||
double Latitude = transferDestination.Latitude;
|
||||
double Longitude = transferDestination.Longitude;
|
||||
string Address = transferDestination.Address;
|
||||
string? Name = transferDestination.Name;
|
||||
string? Description = transferDestination.Description;
|
||||
double? Latitude = transferDestination.Latitude;
|
||||
double? Longitude = transferDestination.Longitude;
|
||||
string? Address = transferDestination.Address;
|
||||
|
||||
if (Id == null || Latitude == null || Longitude == null || Address == null)
|
||||
if (Id == Guid.Empty || Latitude == null || Longitude == null)
|
||||
{
|
||||
return BadRequest("Invalid request");
|
||||
}
|
||||
|
|
@ -100,11 +102,17 @@ namespace TIAMWebApp.Server.Controllers
|
|||
Console.WriteLine($"TransferDestination to be created: {Longitude}");
|
||||
Console.WriteLine($"TransferDestination to be created: {Address}");
|
||||
|
||||
await _transferDestinationDal.Ctx.TransferDestinations.AddRangeAsync(transferDestination);
|
||||
//await _transferDestinationDal.Ctx.TransferDestinations.AddAsync(transferDestination);
|
||||
await _transferDestinationDal.CreateTransferDestinationAsync(transferDestination);
|
||||
return Ok("yes");
|
||||
}
|
||||
|
||||
}
|
||||
return Ok("yes");
|
||||
else
|
||||
{
|
||||
return BadRequest("Invalid request");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -35,12 +35,12 @@ namespace TIAMWebApp.Server.Controllers
|
|||
PasswordHasher hasher = new PasswordHasher();
|
||||
|
||||
|
||||
private UserModel[] users = new UserModel[]
|
||||
/*private UserModel[] users = new UserModel[]
|
||||
{
|
||||
new UserModel(new Guid("540271f6-c604-4c16-8160-d5a7cafedf00"), "test@tiam.hu", "+36701234567", "Asdasd123456"),
|
||||
new UserModel(new Guid("4cbaed43-2465-4d99-84f1-c8bc6b7025f7"), "adam@tiam.hu", "+36701234567", "Asdasd987654")
|
||||
|
||||
};
|
||||
};*/
|
||||
|
||||
private readonly ILogger<UserAPIController> _logger;
|
||||
|
||||
|
|
@ -97,8 +97,8 @@ namespace TIAMWebApp.Server.Controllers
|
|||
//if (userModel == null) return Unauthorized();
|
||||
Console.WriteLine(authenticateUser.Email);
|
||||
|
||||
var dbUser = await GetUserByEmail(authenticateUser.Email);
|
||||
|
||||
//var dbUser = await GetUserByEmail(authenticateUser.Email);
|
||||
var dbUser = await _userDal.GetUserByEmailAsync(authenticateUser.Email);
|
||||
|
||||
//check if password is valid
|
||||
//bool isValidUser = await _userManager.CheckPasswordAsync(userModel, authenticateUser.Password);
|
||||
|
|
@ -293,8 +293,7 @@ namespace TIAMWebApp.Server.Controllers
|
|||
{
|
||||
RegistrationModel? user = JObject.Parse(SerializedRegistrationModel.GetRawText()).ToObject<RegistrationModel>();
|
||||
|
||||
if (users != null)
|
||||
{
|
||||
|
||||
if(user != null)
|
||||
{
|
||||
//add userModel to users array
|
||||
|
|
@ -322,11 +321,7 @@ namespace TIAMWebApp.Server.Controllers
|
|||
}
|
||||
}
|
||||
return Ok("yes");
|
||||
}
|
||||
else
|
||||
{
|
||||
return NotFound("no");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ var builder = WebApplication.CreateBuilder(args);
|
|||
builder.Services.AddControllersWithViews();
|
||||
builder.Services.AddRazorPages();
|
||||
//builder.Services.AddDbContext<TransferDestinationDbContext>(options => options.UseSqlServer(builder.Configuration.GetConnectionString("DeveloperDbConnection")));;
|
||||
builder.Services.AddScoped<UserDal>();
|
||||
builder.Services.AddScoped<AdminDal>();
|
||||
builder.Services.AddScoped<TransferDestinationDal>();
|
||||
|
||||
|
|
|
|||
|
|
@ -16,6 +16,8 @@ namespace TIAMWebApp.Shared.Application.Models
|
|||
public const string RefreshToken = "/api/UserAPI/RefreshToken";
|
||||
public const string WeatherForecast = "api/WeatherForecastAPI";
|
||||
public const string PopulationStructure = "PopulationStructureAPI";
|
||||
public const string GetTransferDestinations = "api/GetTransferDestinations";
|
||||
public const string GetTransferDestinations = "api/TransferDataAPI/GetTransferDestinations";
|
||||
public const string GetTransferDestinationByCoordinates = "api/TransferDataAPI/GetTransferDestinationByCoordinates";
|
||||
public const string GetTransferDestinationByAddress = "api/TransferDataAPI/GetTransferDestinationByAddress";
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue