diff --git a/ProjectLighthouse.Tests/Tests/DatabaseTests.cs b/ProjectLighthouse.Tests/Tests/DatabaseTests.cs new file mode 100644 index 00000000..113e2a63 --- /dev/null +++ b/ProjectLighthouse.Tests/Tests/DatabaseTests.cs @@ -0,0 +1,15 @@ +using System; +using System.Threading.Tasks; + +namespace ProjectLighthouse.Tests { + public class DatabaseTests : LighthouseTest { + [DatabaseFact] + public async Task CanCreateUserTwice() { + await using Database database = new(); + int rand = new Random().Next(); + + await database.CreateUser("createUserTwiceTest" + rand); + await database.CreateUser("createUserTwiceTest" + rand); + } + } +} \ No newline at end of file diff --git a/ProjectLighthouse.Tests/Tests/SlotTests.cs b/ProjectLighthouse.Tests/Tests/SlotTests.cs new file mode 100644 index 00000000..63933093 --- /dev/null +++ b/ProjectLighthouse.Tests/Tests/SlotTests.cs @@ -0,0 +1,50 @@ +using System.Collections.Generic; +using System.Net.Http; +using System.Threading.Tasks; +using ProjectLighthouse.Types; +using Xunit; + +namespace ProjectLighthouse.Tests { + public class SlotTests : LighthouseTest { + [DatabaseFact] + public async Task ShouldOnlyShowUsersLevels() { + await using Database database = new(); + + User userA = await database.CreateUser("unitTestUser0"); + User userB = await database.CreateUser("unitTestUser1"); + + Location l = new(); + database.Locations.Add(l); + await database.SaveChangesAsync(); + + Slot slotA = new() { + Creator = userA, + Name = "slotA", + Location = l, + LocationId = l.Id, + }; + + Slot slotB = new() { + Creator = userB, + Name = "slotB", + Location = l, + LocationId = l.Id, + }; + + database.Slots.Add(slotA); + database.Slots.Add(slotB); + + await database.SaveChangesAsync(); + +// XmlSerializer serializer = new(typeof(Slot)); +// Slot slot = (Slot)serializer.Deserialize(new StringReader(bodyString)); + + string respA = await this.Client.GetStringAsync("LITTLEBIGPLANETPS3_XML/slots/by?u=unitTestUser0"); + string respB = await this.Client.GetStringAsync("LITTLEBIGPLANETPS3_XML/slots/by?u=unitTestUser1"); + + Assert.NotEqual(respA, respB); + Assert.DoesNotContain(respA, "slotB"); + Assert.DoesNotContain(respB, "slotA"); + } + } +} \ No newline at end of file diff --git a/ProjectLighthouse/Database.cs b/ProjectLighthouse/Database.cs index 8700e32a..0cd18e70 100644 --- a/ProjectLighthouse/Database.cs +++ b/ProjectLighthouse/Database.cs @@ -1,3 +1,4 @@ +using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.EntityFrameworkCore; @@ -22,11 +23,15 @@ namespace ProjectLighthouse { ); public async Task CreateUser(string username) { + User user; + if((user = await Users.Where(u => u.Username == username).FirstOrDefaultAsync()) != null) + return user; + Location l = new(); // store to get id after submitting this.Locations.Add(l); // add to table await this.SaveChangesAsync(); // saving to the database returns the id and sets it on this entity - User user = new() { + user = new() { Username = username, LocationId = l.Id, Biography = username + " hasn't introduced themselves yet.", @@ -36,7 +41,6 @@ namespace ProjectLighthouse { await this.SaveChangesAsync(); return user; - } #nullable enable