-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #271 from SCCapstone/main
Deploy site
- Loading branch information
Showing
74 changed files
with
10,586 additions
and
2,131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
namespace FU.API.Tests; | ||
|
||
using FU.API.Data; | ||
using FU.API.Models; | ||
using FU.API.Services; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.Configuration; | ||
|
||
public class AccountServiceTests : IDisposable | ||
{ | ||
private readonly DbContextOptions<AppDbContext> _contextOptions; | ||
private readonly AppDbContext _dbContext; | ||
private readonly AccountsService _accountsService; | ||
|
||
// adapted from https://github.com/dotnet/EntityFramework.Docs/blob/main/samples/core/Testing/TestingWithoutTheDatabase/InMemoryBloggingControllerTest.cs | ||
public AccountServiceTests() | ||
{ | ||
_contextOptions = new DbContextOptionsBuilder<AppDbContext>() | ||
.UseInMemoryDatabase("AccountServiceTestsDb") | ||
.Options; | ||
|
||
_dbContext = new AppDbContext(_contextOptions); | ||
|
||
_dbContext.Database.EnsureDeleted(); | ||
_dbContext.Database.EnsureCreated(); | ||
|
||
_dbContext.SaveChanges(); | ||
|
||
// Setup and create account service | ||
var configPairs = new Dictionary<string, string?> { { "JWT_SECRET", "1234567890" } }; | ||
var configuration = new ConfigurationBuilder() | ||
.AddInMemoryCollection(configPairs) | ||
.Build(); | ||
_accountsService = new AccountsService(configuration, _dbContext); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_dbContext.Dispose(); | ||
} | ||
|
||
[Fact] | ||
public async void CreateUser_WithValidCredentials_ReturnsUser() | ||
{ | ||
// Arange | ||
Credentials credentials = new() { Username = "Test", Password = "Test" }; | ||
|
||
// Act | ||
ApplicationUser user = await _accountsService.Register(credentials); | ||
|
||
// Assert | ||
Assert.Equal(user.Username, credentials.Username); | ||
Assert.NotNull(user.PasswordHash); | ||
} | ||
|
||
[Fact] | ||
public async void ChangeUsername_WithValidUsername_ChangesUsername() | ||
{ | ||
// Arange | ||
Credentials credentials = new() { Username = "Username1", Password = "Test" }; | ||
string newUsername = "Username2"; | ||
ApplicationUser user = await _accountsService.Register(credentials); | ||
|
||
// Act | ||
await _accountsService.UpdateUsername(user.UserId, newUsername); | ||
|
||
// Assert | ||
Assert.Equal(user.Username, newUsername); | ||
} | ||
|
||
[Fact] | ||
public async void ChangePassword_WithValidPassword_ChangesPassword() | ||
{ | ||
// Arange | ||
Credentials credentials = new() { Username = "Username1", Password = "Password1" }; | ||
string newPassword = "Password2"; | ||
ApplicationUser user = await _accountsService.Register(credentials); | ||
var ogPasswordHash = user.PasswordHash; | ||
|
||
// Act | ||
await _accountsService.UpdatePassword(user.UserId, newPassword); | ||
|
||
// Assert | ||
Assert.NotEqual(user.PasswordHash, ogPasswordHash); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
namespace FU.API.Tests.Helpers; | ||
|
||
using FU.API.Models; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
public static class MockDataHelper | ||
{ | ||
public static List<ApplicationUser> CreateTestUsers() | ||
{ | ||
return new List<ApplicationUser>() | ||
{ | ||
new ApplicationUser() | ||
{ | ||
UserId = 1, | ||
Username = "User1", | ||
}, | ||
new ApplicationUser() | ||
{ | ||
UserId = 2, | ||
Username = "User2", | ||
}, | ||
new ApplicationUser() | ||
{ | ||
UserId = 3, | ||
Username = "User3", | ||
}, | ||
new ApplicationUser() | ||
{ | ||
UserId = 4, | ||
Username = "User4", | ||
}, | ||
new ApplicationUser() | ||
{ | ||
UserId = 5, | ||
Username = "User5", | ||
}, | ||
new ApplicationUser() | ||
{ | ||
UserId = 6, | ||
Username = "User6", | ||
}, | ||
}; | ||
} | ||
|
||
public static Chat CreateTestChat(List<ApplicationUser> users) | ||
{ | ||
return new Chat() | ||
{ | ||
Id = 1, | ||
ChatType = ChatType.Post, | ||
ChatName = "Title1", | ||
CreatorId = 1, | ||
Members = users.Select(u => new ChatMembership() | ||
{ | ||
ChatId = 1, | ||
UserId = u.UserId, | ||
User = u, | ||
}).ToList(), | ||
}; | ||
} | ||
|
||
public static List<UserRelation> CreateTestRelations(List<ApplicationUser> users) | ||
{ | ||
return new List<UserRelation>() | ||
{ | ||
new UserRelation() | ||
{ | ||
User1 = users[0], | ||
User2 = users[1], | ||
Status = UserRelationStatus.Blocked, | ||
}, | ||
new UserRelation() | ||
{ | ||
User1 = users[1], | ||
User2 = users[0], | ||
Status = UserRelationStatus.BlockedBy, | ||
}, | ||
new UserRelation() | ||
{ | ||
User1 = users[2], | ||
User2 = users[3], | ||
Status = UserRelationStatus.Requested, | ||
}, | ||
new UserRelation() | ||
{ | ||
User1 = users[3], | ||
User2 = users[2], | ||
Status = UserRelationStatus.Pending, | ||
}, | ||
new UserRelation() | ||
{ | ||
User1 = users[4], | ||
User2 = users[5], | ||
Status = UserRelationStatus.Friends, | ||
}, | ||
new UserRelation() | ||
{ | ||
User1 = users[5], | ||
User2 = users[4], | ||
Status = UserRelationStatus.Friends, | ||
}, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
namespace FU.API.Tests; | ||
|
||
using FU.API.Data; | ||
using FU.API.Models; | ||
using FU.API.Services; | ||
using FU.API.Tests.Helpers; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
public class PostServiceTests | ||
{ | ||
private readonly DbContextOptions<AppDbContext> _contextOptions; | ||
|
||
// adapted from https://github.com/dotnet/EntityFramework.Docs/blob/main/samples/core/Testing/TestingWithoutTheDatabase/InMemoryBloggingControllerTest.cs | ||
public PostServiceTests() | ||
{ | ||
_contextOptions = new DbContextOptionsBuilder<AppDbContext>() | ||
.UseInMemoryDatabase("PostServiceTestDb") | ||
.Options; | ||
|
||
using var context = new AppDbContext(_contextOptions); | ||
|
||
context.Database.EnsureDeleted(); | ||
context.Database.EnsureCreated(); | ||
|
||
context.SaveChanges(); | ||
} | ||
|
||
[Theory] | ||
[InlineData(1, 1, true)] | ||
[InlineData(1, 2, true)] | ||
[InlineData(1, 9, false)] | ||
public async void GetPostUsers_WithValidPostId_CheckUserJoined(int postId, int checkUserId, bool expectedJoined) | ||
{ | ||
// Arrange | ||
var context = CreateContext(); | ||
|
||
var testUsers = MockDataHelper.CreateTestUsers(); | ||
var testChat = MockDataHelper.CreateTestChat(testUsers); | ||
context.Set<Chat>().Add(testChat); | ||
context.Set<ApplicationUser>().AddRange(testUsers); | ||
|
||
var post = new Post() | ||
{ | ||
Id = 1, | ||
Title = "TestTitle", | ||
GameId = 1, | ||
CreatorId = testUsers[0].UserId, | ||
Creator = testUsers[0], | ||
ChatId = testChat.Id, | ||
Chat = testChat, | ||
}; | ||
|
||
context.Set<Post>().Add(post); | ||
context.SaveChanges(); | ||
|
||
|
||
var chatService = new ChatService(context); | ||
var postService = new PostService(context, chatService); | ||
|
||
// Act | ||
var postUsers = await postService.GetPostUsers(postId); | ||
|
||
// Assert | ||
var joined = postUsers.Any(u => u.UserId == checkUserId); | ||
Assert.Equal(expectedJoined, joined); | ||
Assert.Equal(6, postUsers.Count()); | ||
} | ||
|
||
AppDbContext CreateContext() => new(_contextOptions); | ||
|
||
[Fact] | ||
public async void CreatePost_WithValidParams_ReturnsCreated() | ||
{ | ||
// Arrange | ||
using var context = CreateContext(); | ||
var gameService = new GameService(context); | ||
var chatService = new ChatService(context); | ||
var postService = new PostService(context, chatService); | ||
ApplicationUser user = await TestsHelper.CreateUserAsync(context); | ||
|
||
// Act | ||
Game game = await gameService.CreateGame("Game Title"); | ||
Post post = new() | ||
{ | ||
Title = "Title Text", | ||
Description = "Description Text", | ||
GameId = game.Id, | ||
Creator = user, | ||
CreatorId = user.UserId, | ||
}; | ||
var createdPost = await postService.CreatePost(post); | ||
|
||
// Assert | ||
Assert.Equal(post.Title, createdPost.Title); | ||
Assert.Equal(post.Description, createdPost.Description); | ||
} | ||
|
||
[Fact] | ||
public async void UpdatePost_WithValidParams_ReturnsUpdated() | ||
{ | ||
// Arrange | ||
using var context = CreateContext(); | ||
var gameService = new GameService(context); | ||
var chatService = new ChatService(context); | ||
var postService = new PostService(context, chatService); | ||
ApplicationUser user = await TestsHelper.CreateUserAsync(context); | ||
|
||
Game game = await gameService.CreateGame("Game Title"); | ||
Post post = new() | ||
{ | ||
Title = "Title Text", | ||
Description = "Description Text", | ||
GameId = game.Id, | ||
Creator = user, | ||
CreatorId = user.UserId, | ||
}; | ||
var createdPost = await postService.CreatePost(post); | ||
|
||
// Act | ||
createdPost.Description = "Description Text 2"; | ||
var updatedPost = await postService.UpdatePost(createdPost); | ||
|
||
// Assert | ||
Assert.Equal(createdPost.Description, updatedPost.Description); | ||
} | ||
} |
Oops, something went wrong.