Skip to content

Commit

Permalink
Merge pull request #433 from SCCapstone/main
Browse files Browse the repository at this point in the history
Redeploy for testing purposes
  • Loading branch information
evan-scales authored Mar 24, 2024
2 parents f50bc87 + 6df5547 commit 6e0baba
Show file tree
Hide file tree
Showing 123 changed files with 6,294 additions and 2,099 deletions.
9 changes: 7 additions & 2 deletions FU.API/FU.API.Tests/AccountServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,16 @@ public AccountServiceTests()
_dbContext.SaveChanges();

// Setup and create account service
var configPairs = new Dictionary<string, string?> { { "JWT_SECRET", "1234567890" } };
var configPairs = new Dictionary<string, string?>
{
{ "JWT_SECRET", "1234567890" },
{ "EMAIL_CONNECTION_STRING", "endpoint=https://fake.com/;accesskey=Pdada/dsadasd==" },
{ "BASE_SPA_URL", "http://localhost:5173/" },
};
var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(configPairs)
.Build();
_accountsService = new AccountsService(configuration, _dbContext);
_accountsService = new AccountsService(configuration, _dbContext, new EmailService(configuration));
}

public void Dispose()
Expand Down
82 changes: 74 additions & 8 deletions FU.API/FU.API.Tests/GameServiceTests.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
namespace FU.API.Tests;

using FU.API.Data;
using FU.API.Models;
using FU.API.Services;
using Microsoft.EntityFrameworkCore;

public class GameServiceTests
public class GameServiceTests : IDisposable
{
private readonly DbContextOptions<AppDbContext> _contextOptions;
private readonly AppDbContext _dbContext;
private readonly GameService _gameService;

// adapted from https://github.com/dotnet/EntityFramework.Docs/blob/main/samples/core/Testing/TestingWithoutTheDatabase/InMemoryBloggingControllerTest.cs
public GameServiceTests()
Expand All @@ -17,29 +20,92 @@ public GameServiceTests()

using var context = new AppDbContext(_contextOptions);

// Setup Db
context.Database.EnsureDeleted();
context.Database.EnsureCreated();

context.SaveChanges();
}

AppDbContext CreateContext() => new(_contextOptions);
// Setup and create a game service
_dbContext = new(_contextOptions);
_gameService = new(_dbContext);
}

[Theory]
[InlineData("")]
[InlineData("Title1")]
[InlineData("Title with space")]
[InlineData("Title with symbols :-={}<>.")]
public async void CreateGame_WithValidString_ReturnsGame(string gameName)
{
// Act
var game = await _gameService.CreateGame(gameName);

// Assert
Assert.Equal(game.Name, gameName);
}

[Fact]
public async void DeleteGame_WithValidGame_DeletesGame()
{
// Arrange
using var context = CreateContext();
var gameService = new GameService(context);
Game game = await _gameService.CreateGame("game1");

// Act
var game = await gameService.CreateGame(gameName);
await _gameService.DeleteGame(game);


// Assert
Assert.Equal(game.Name, gameName);
Assert.Null(await _gameService.GetGame(game.Id));
}

[Fact]
public async void GetGame_WithValidGame_GetsGame()
{
// Arrange
Game createdGame = await _gameService.CreateGame("game1");

// Act
Game? fetchedGame = await _gameService.GetGame(createdGame.Id);

// Assert
Assert.NotNull(fetchedGame);
}

[Theory]
[InlineData("gameC", 0)]
[InlineData("gameB", 2)]
[InlineData("", 3)]
public async void GetGames_WithGames_ReturnCorrectNumResults(string queryString, int numResults)
{
// Arrange
await _gameService.CreateGame("gameA1");
await _gameService.CreateGame("gameB2");
await _gameService.CreateGame("gameB3");

// Act
var fetchedGames = await _gameService.GetGames(queryString);

// Assert
Assert.Equal(numResults, fetchedGames.Count());
}

[Fact]
public async void UpdateGame_WithValidGame_UpdatesGameTitle()
{
// Arrange
var game = await _gameService.CreateGame("gameA1");
game.Name = "gameB2";

// Act
await _gameService.UpdateGame(game);

// Assert
var fetchedGame = await _gameService.GetGame(game.Id);
Assert.Equal(game.Name, fetchedGame?.Name);
}

public void Dispose()
{
_dbContext.Dispose();
}
}
3 changes: 0 additions & 3 deletions FU.API/FU.API.Tests/Helpers/MockDataHelper.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
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
{
Expand Down
144 changes: 108 additions & 36 deletions FU.API/FU.API.Tests/PostServiceTests.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
namespace FU.API.Tests;

using FU.API.Data;
using FU.API.Exceptions;
using FU.API.Models;
using FU.API.Services;
using FU.API.Tests.Helpers;
using Microsoft.EntityFrameworkCore;

public class PostServiceTests
public class PostServiceTests : IDisposable
{
private readonly DbContextOptions<AppDbContext> _contextOptions;
private readonly AppDbContext _dbContext;
private readonly PostService _postService;

// adapted from https://github.com/dotnet/EntityFramework.Docs/blob/main/samples/core/Testing/TestingWithoutTheDatabase/InMemoryBloggingControllerTest.cs
public PostServiceTests()
{
// Setup database
_contextOptions = new DbContextOptionsBuilder<AppDbContext>()
.UseInMemoryDatabase("PostServiceTestDb")
.Options;

using var context = new AppDbContext(_contextOptions);

context.Database.EnsureDeleted();
context.Database.EnsureCreated();

context.SaveChanges();
_dbContext = new AppDbContext(_contextOptions);
_dbContext.Database.EnsureDeleted();
_dbContext.Database.EnsureCreated();
_dbContext.SaveChanges();

// Setup PostServce
_dbContext = new(_contextOptions);
var chatService = new ChatService(_dbContext);
_postService = new PostService(_dbContext, chatService);
}

[Theory]
Expand All @@ -32,12 +38,10 @@ public PostServiceTests()
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);
_dbContext.Set<Chat>().Add(testChat);
_dbContext.Set<ApplicationUser>().AddRange(testUsers);

var post = new Post()
{
Expand All @@ -50,33 +54,24 @@ public async void GetPostUsers_WithValidPostId_CheckUserJoined(int postId, int c
Chat = testChat,
};

context.Set<Post>().Add(post);
context.SaveChanges();


var chatService = new ChatService(context);
var postService = new PostService(context, chatService);
_dbContext.Set<Post>().Add(post);
_dbContext.SaveChanges();

// Act
var postUsers = await postService.GetPostUsers(postId);
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);
var gameService = new GameService(_dbContext);
ApplicationUser user = await TestsHelper.CreateUserAsync(_dbContext);

// Act
Game game = await gameService.CreateGame("Game Title");
Expand All @@ -88,7 +83,7 @@ public async void CreatePost_WithValidParams_ReturnsCreated()
Creator = user,
CreatorId = user.UserId,
};
var createdPost = await postService.CreatePost(post);
var createdPost = await _postService.CreatePost(post);

// Assert
Assert.Equal(post.Title, createdPost.Title);
Expand All @@ -99,12 +94,27 @@ public async void CreatePost_WithValidParams_ReturnsCreated()
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);
var createdPost = await TestsHelper.CreateTestPostAsync(_dbContext);

// Act
createdPost.Description = "Description Text 2";
var updatedPost = await _postService.UpdatePost(createdPost);

// Assert
Assert.Equal(createdPost.Description, updatedPost.Description);
}

[Theory]
[InlineData("2022-01-01T00:00:00", "2022-01-01T00:00:00")] // Date in the past
[InlineData("2025-01-01T00:00:01", "2025-01-01T00:00:00")] // End time before start time
[InlineData("9999-01-01T00:00:00", "9999-01-01T00:00:01")] // Date far in the future
public async void CreatePost_InvalidDate(DateTime startTime, DateTime endTime)
{
// Arrange
var gameService = new GameService(_dbContext);
ApplicationUser user = await TestsHelper.CreateUserAsync(_dbContext);

// Act
Game game = await gameService.CreateGame("Game Title");
Post post = new()
{
Expand All @@ -113,14 +123,76 @@ public async void UpdatePost_WithValidParams_ReturnsUpdated()
GameId = game.Id,
Creator = user,
CreatorId = user.UserId,
StartTime = startTime,
EndTime = endTime,
};
var createdPost = await postService.CreatePost(post);

// Calling create post should throw a PostException
await Assert.ThrowsAsync<PostException>(async () => await _postService.CreatePost(post));
}

[Fact]
public async void GetPostUsers_WithMultipleUsers_ReturnsCorrectUserCount()
{
// Arrange
// Note: CreateTestPostAsync creates one user as part of creating a post
Post post = await TestsHelper.CreateTestPostAsync(_dbContext);
var user2 = await TestsHelper.CreateUserAsync(_dbContext, new Credentials() { Username = "user2", Password = "pass2", Email = "[email protected]" });
await _postService.JoinPost(post.Id, user2);

// Act
createdPost.Description = "Description Text 2";
var updatedPost = await postService.UpdatePost(createdPost);
var posts = await _postService.GetPostUsers(post.Id);

// Assert
Assert.Equal(createdPost.Description, updatedPost.Description);
Assert.Equal(2, posts.Count());
}

[Fact]
public async void DeletePost_WithValidRequest_DeletesPost()
{
// Arrange
Post post = await TestsHelper.CreateTestPostAsync(_dbContext);

// Act
await _postService.DeletePost(post.Id);

// Assert
var posts = await _postService.GetPostUsers(post.Id);
Assert.Empty(posts);
}

[Fact]
public async void JoinPost_WhenAlreadyMember_ThrowsConflictException()
{
// Arrange
Post post = await TestsHelper.CreateTestPostAsync(_dbContext);
var user2 = await TestsHelper.CreateUserAsync(_dbContext, new Credentials() { Username = "user2", Password = "pass2", Email = "[email protected]" });
await _postService.JoinPost(post.Id, user2);

// Act & Assert
await Assert.ThrowsAnyAsync<ExceptionWithResponse>(async () =>
await _postService.JoinPost(post.Id, user2));

}

[Fact]
public async void LeavePost_WhenMember_LeavesPost()
{
// Arrange
Post post = await TestsHelper.CreateTestPostAsync(_dbContext);
var user2 = await TestsHelper.CreateUserAsync(_dbContext, new Credentials() { Username = "user2", Password = "pass2", Email = "[email protected]" });
await _postService.JoinPost(post.Id, user2);

// Act
await _postService.LeavePost(post.Id, user2);

// Assert
var posts = await _postService.GetPostUsers(post.Id);
Assert.DoesNotContain(user2, posts);
}

public void Dispose()
{
_dbContext.Dispose();
}
}
1 change: 0 additions & 1 deletion FU.API/FU.API.Tests/RelationServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using FU.API.Services;
using FU.API.Tests.Helpers;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;

public class RelationServiceTests
{
Expand Down
Loading

0 comments on commit 6e0baba

Please sign in to comment.