Skip to content

Commit

Permalink
Merge pull request #271 from SCCapstone/main
Browse files Browse the repository at this point in the history
Deploy site
  • Loading branch information
evan-scales authored Feb 11, 2024
2 parents 1706dd2 + 66d620e commit acc988a
Show file tree
Hide file tree
Showing 74 changed files with 10,586 additions and 2,131 deletions.
27 changes: 15 additions & 12 deletions .github/workflows/build-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

name: Build ForcesUnite

on: push
on:
workflow_dispatch:
pull_request:
types: [ready_for_review]

jobs:
build-api:
Expand All @@ -23,17 +26,17 @@ jobs:
dotnet build
shell: pwsh

- name: Publish
run: |
cd FU.API
dotnet publish
shell: pwsh

- name: Upload Artifacts
uses: actions/upload-artifact@v3
with:
name: .net-app
path: D:\a\PalmettoProgrammers\PalmettoProgrammers\FU.API\FU.API\bin
# - name: Publish
# run: |
# cd FU.API
# dotnet publish
# shell: pwsh

# - name: Upload Artifacts
# uses: actions/upload-artifact@v3
# with:
# name: .net-app
# path: D:\a\PalmettoProgrammers\PalmettoProgrammers\FU.API\FU.API\bin

- name: Test
run: |
Expand Down
4 changes: 4 additions & 0 deletions FU.API/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ dotnet_diagnostic.SA1000.severity = none
# SA1201: Elements should appear in the correct order
dotnet_diagnostic.SA1201.severity = none

# Closing parans should not be preceded by a space
# Disabled for long condition clarity
dotnet_diagnostic.SA1009.severity = none

dotnet_diagnostic.SA1309.severity = none
dotnet_diagnostic.SA1101.severity = none
dotnet_diagnostic.SA1600.severity = none
Expand Down
86 changes: 86 additions & 0 deletions FU.API/FU.API.Tests/AccountServiceTests.cs
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);
}
}
1 change: 1 addition & 0 deletions FU.API/FU.API.Tests/FU.API.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.11" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.1" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.analyzers" Version="1.10.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down
108 changes: 108 additions & 0 deletions FU.API/FU.API.Tests/Helpers/MockDataHelper.cs
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,
},
};
}
}
126 changes: 126 additions & 0 deletions FU.API/FU.API.Tests/PostServiceTests.cs
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);
}
}
Loading

0 comments on commit acc988a

Please sign in to comment.