Skip to content

Commit

Permalink
Merge branch 'master' into site-settings-proj-sort
Browse files Browse the repository at this point in the history
  • Loading branch information
imnasnainaec authored Oct 9, 2023
2 parents 447acbc + 7063cb2 commit 2712a5d
Show file tree
Hide file tree
Showing 56 changed files with 1,470 additions and 278 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -129,19 +129,19 @@ jobs:
with:
dotnet-version: "6.0.x"
- name: Initialize CodeQL
uses: github/codeql-action/init@6a28655e3dcb49cb0840ea372fd6d17733edd8a4 # v2.21.8
uses: github/codeql-action/init@ddccb873888234080b77e9bc2d4764d5ccaaccf9 # v2.21.9
with:
languages: csharp
- name: Autobuild
uses: github/codeql-action/autobuild@6a28655e3dcb49cb0840ea372fd6d17733edd8a4 # v2.21.8
uses: github/codeql-action/autobuild@ddccb873888234080b77e9bc2d4764d5ccaaccf9 # v2.21.9
- name: Upload artifacts if build failed
uses: actions/upload-artifact@a8a3f3ad30e3422c9c7b888a15615d19a852ae32 # v3.1.3
if: ${{ failure() }}
with:
name: tracer-logs
path: ${{ runner.temp }}/*.log
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@6a28655e3dcb49cb0840ea372fd6d17733edd8a4 # v2.21.8
uses: github/codeql-action/analyze@ddccb873888234080b77e9bc2d4764d5ccaaccf9 # v2.21.9

docker_build:
runs-on: ubuntu-22.04
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ jobs:

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@6a28655e3dcb49cb0840ea372fd6d17733edd8a4 # v2.21.8
uses: github/codeql-action/init@ddccb873888234080b77e9bc2d4764d5ccaaccf9 # v2.21.9
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
Expand All @@ -76,7 +76,7 @@ jobs:
# Autobuild attempts to build any compiled languages (C/C++, C#, Go, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@6a28655e3dcb49cb0840ea372fd6d17733edd8a4 # v2.21.8
uses: github/codeql-action/autobuild@ddccb873888234080b77e9bc2d4764d5ccaaccf9 # v2.21.9

# Command-line programs to run using the OS shell.
# See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun
Expand All @@ -89,6 +89,6 @@ jobs:
# ./location_of_script_within_repo/buildscript.sh

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@6a28655e3dcb49cb0840ea372fd6d17733edd8a4 # v2.21.8
uses: github/codeql-action/analyze@ddccb873888234080b77e9bc2d4764d5ccaaccf9 # v2.21.9
with:
category: "/language:${{matrix.language}}"
2 changes: 1 addition & 1 deletion .github/workflows/scorecards.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,6 @@ jobs:

# Upload the results to GitHub's code scanning dashboard.
- name: "Upload to code-scanning"
uses: github/codeql-action/upload-sarif@6a28655e3dcb49cb0840ea372fd6d17733edd8a4 # v2.21.8
uses: github/codeql-action/upload-sarif@ddccb873888234080b77e9bc2d4764d5ccaaccf9 # v2.21.9
with:
sarif_file: results.sarif
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"Dups",
"endcap",
"globaltool",
"graylist",
"Guids",
"kubeconfig",
"langtags",
Expand Down
33 changes: 29 additions & 4 deletions Backend.Tests/Controllers/MergeControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace Backend.Tests.Controllers
public class MergeControllerTests : IDisposable
{
private IMergeBlacklistRepository _mergeBlacklistRepo = null!;
private IMergeGraylistRepository _mergeGraylistRepo = null!;
private IWordRepository _wordRepo = null!;
private IMergeService _mergeService = null!;
private IPermissionService _permissionService = null!;
Expand All @@ -38,9 +39,10 @@ protected virtual void Dispose(bool disposing)
public void Setup()
{
_mergeBlacklistRepo = new MergeBlacklistRepositoryMock();
_mergeGraylistRepo = new MergeGraylistRepositoryMock();
_wordRepo = new WordRepositoryMock();
_wordService = new WordService(_wordRepo);
_mergeService = new MergeService(_mergeBlacklistRepo, _wordRepo, _wordService);
_mergeService = new MergeService(_mergeBlacklistRepo, _mergeGraylistRepo, _wordRepo, _wordService);
_permissionService = new PermissionServiceMock();
_mergeController = new MergeController(_mergeService, _permissionService);
}
Expand All @@ -54,16 +56,39 @@ public void BlacklistAddTest()

// Add two Lists of wordIds.
_ = _mergeController.BlacklistAdd(ProjId, wordIdsA).Result;
var result = _mergeBlacklistRepo.GetAllEntries(ProjId).Result;
var result = _mergeBlacklistRepo.GetAllSets(ProjId).Result;
Assert.That(result, Has.Count.EqualTo(1));
Assert.That(result.First().WordIds, Is.EqualTo(wordIdsA));
_ = _mergeController.BlacklistAdd(ProjId, wordIdsB).Result;
result = _mergeBlacklistRepo.GetAllEntries(ProjId).Result;
result = _mergeBlacklistRepo.GetAllSets(ProjId).Result;
Assert.That(result, Has.Count.EqualTo(2));

// Add a List of wordIds that contains both previous lists.
_ = _mergeController.BlacklistAdd(ProjId, wordIdsC).Result;
result = _mergeBlacklistRepo.GetAllEntries(ProjId).Result;
result = _mergeBlacklistRepo.GetAllSets(ProjId).Result;
Assert.That(result, Has.Count.EqualTo(1));
Assert.That(result.First().WordIds, Is.EqualTo(wordIdsC));
}

[Test]
public void GreylistAddTest()
{
var wordIdsA = new List<string> { "1", "2" };
var wordIdsB = new List<string> { "3", "1" };
var wordIdsC = new List<string> { "1", "2", "3" };

// Add two Lists of wordIds.
_ = _mergeController.GraylistAdd(ProjId, wordIdsA).Result;
var result = _mergeGraylistRepo.GetAllSets(ProjId).Result;
Assert.That(result, Has.Count.EqualTo(1));
Assert.That(result.First().WordIds, Is.EqualTo(wordIdsA));
_ = _mergeController.GraylistAdd(ProjId, wordIdsB).Result;
result = _mergeGraylistRepo.GetAllSets(ProjId).Result;
Assert.That(result, Has.Count.EqualTo(2));

// Add a List of wordIds that contains both previous lists.
_ = _mergeController.GraylistAdd(ProjId, wordIdsC).Result;
result = _mergeGraylistRepo.GetAllSets(ProjId).Result;
Assert.That(result, Has.Count.EqualTo(1));
Assert.That(result.First().WordIds, Is.EqualTo(wordIdsC));
}
Expand Down
172 changes: 172 additions & 0 deletions Backend.Tests/Controllers/StatisticsControllerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
using System;
using System.Threading.Tasks;
using Backend.Tests.Mocks;
using BackendFramework.Controllers;
using BackendFramework.Interfaces;
using BackendFramework.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NUnit.Framework;

namespace Backend.Tests.Controllers
{
public class StatisticsControllerTests : IDisposable
{
private IProjectRepository _projRepo = null!;
private IUserRepository _userRepo = null!;
private IPermissionService _permService = null!;
private StatisticsController _statsController = null!;

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (disposing)
{
_statsController?.Dispose();
}
}

private User _jwtAuthenticatedUser = null!;
private string _projId = null!;
private const string MissingId = "MISSING_ID";

[SetUp]
public async Task Setup()
{
_projRepo = new ProjectRepositoryMock();
_userRepo = new UserRepositoryMock();
_permService = new PermissionServiceMock(_userRepo);
_statsController = new StatisticsController(new StatisticsServiceMock(), _permService, _projRepo)
{
// Mock the Http Context because this isn't an actual call controller
ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext() }
};

_jwtAuthenticatedUser = new User { Username = "user", Password = "pass" };
await _userRepo.Create(_jwtAuthenticatedUser);
_jwtAuthenticatedUser = await _permService.Authenticate(_jwtAuthenticatedUser.Username,
_jwtAuthenticatedUser.Password) ?? throw new UserAuthenticationException();
_projId = (await _projRepo.Create(new Project { Name = "StatisticsControllerTests" }))!.Id;
}

[Test]
public async Task TestGetSemanticDomainCountsNoPermission()
{
_statsController.ControllerContext.HttpContext = PermissionServiceMock.UnauthorizedHttpContext();

var result = await _statsController.GetSemanticDomainCounts(_projId, "en");
Assert.That(result, Is.InstanceOf<ForbidResult>());
}

[Test]
public async Task TestGetSemanticDomainCountsMissingProject()
{
var result = await _statsController.GetSemanticDomainCounts(MissingId, "en");
Assert.That(result, Is.InstanceOf<NotFoundObjectResult>());
}

[Test]
public async Task TestGetSemanticDomainCounts()
{
var result = await _statsController.GetSemanticDomainCounts(_projId, "en");
Assert.That(result, Is.InstanceOf<OkObjectResult>());
}

[Test]
public async Task TestGetWordsPerDayPerUserCountsNoPermission()
{
_statsController.ControllerContext.HttpContext = PermissionServiceMock.UnauthorizedHttpContext();

var result = await _statsController.GetWordsPerDayPerUserCounts(_projId);
Assert.That(result, Is.InstanceOf<ForbidResult>());
}

[Test]
public async Task TestGetWordsPerDayPerUserCountsMissingProject()
{
var result = await _statsController.GetWordsPerDayPerUserCounts(MissingId);
Assert.That(result, Is.InstanceOf<NotFoundObjectResult>());
}

[Test]
public async Task TestGetWordsPerDayPerUserCounts()
{
var result = await _statsController.GetWordsPerDayPerUserCounts(_projId);
Assert.That(result, Is.InstanceOf<OkObjectResult>());
}

[Test]
public async Task TestGetProgressEstimationLineChartRootNoPermission()
{
_statsController.ControllerContext.HttpContext = PermissionServiceMock.UnauthorizedHttpContext();

var result = await _statsController.GetProgressEstimationLineChartRoot(_projId);
Assert.That(result, Is.InstanceOf<ForbidResult>());
}

[Test]
public async Task TestGetProgressEstimationLineChartRootMissingProject()
{
var result = await _statsController.GetProgressEstimationLineChartRoot(MissingId);
Assert.That(result, Is.InstanceOf<NotFoundObjectResult>());
}

[Test]
public async Task TestGetProgressEstimationLineChartRoot()
{
var result = await _statsController.GetProgressEstimationLineChartRoot(_projId);
Assert.That(result, Is.InstanceOf<OkObjectResult>());
}

[Test]
public async Task TestGetLineChartRootDataNoPermission()
{
_statsController.ControllerContext.HttpContext = PermissionServiceMock.UnauthorizedHttpContext();

var result = await _statsController.GetLineChartRootData(_projId);
Assert.That(result, Is.InstanceOf<ForbidResult>());
}

[Test]
public async Task TestGetLineChartRootDataMissingProject()
{
var result = await _statsController.GetLineChartRootData(MissingId);
Assert.That(result, Is.InstanceOf<NotFoundObjectResult>());
}

[Test]
public async Task TestGetLineChartRootData()
{
var result = await _statsController.GetLineChartRootData(_projId);
Assert.That(result, Is.InstanceOf<OkObjectResult>());
}

[Test]
public async Task TestGetSemanticDomainUserCountsNoPermission()
{
_statsController.ControllerContext.HttpContext = PermissionServiceMock.UnauthorizedHttpContext();

var result = await _statsController.GetSemanticDomainUserCounts(_projId, "en");
Assert.That(result, Is.InstanceOf<ForbidResult>());
}

[Test]
public async Task TestGetSemanticDomainUserCountsMissingProject()
{
var result = await _statsController.GetSemanticDomainUserCounts(MissingId, "en");
Assert.That(result, Is.InstanceOf<NotFoundObjectResult>());
}

[Test]
public async Task TestGetSemanticDomainUserCounts()
{
var result = await _statsController.GetSemanticDomainUserCounts(_projId, "en");
Assert.That(result, Is.InstanceOf<OkObjectResult>());
}
}
}
18 changes: 9 additions & 9 deletions Backend.Tests/Helper/DuplicateFinderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class DuplicateFinderTests
{
private DuplicateFinder _dupFinder = null!;
private List<Word> _frontier = null!;
private Func<List<string>, Task<bool>> _isInBlacklist = null!;
private Func<List<string>, Task<bool>> _isUnavailableSet = null!;

private const int MaxInList = 4;
private const int MaxLists = 3;
Expand All @@ -26,7 +26,7 @@ public void Setup()
{
_dupFinder = new DuplicateFinder(MaxInList, MaxLists, MaxScore);
_frontier = new List<Word>();
_isInBlacklist = _ => Task.FromResult(false);
_isUnavailableSet = _ => Task.FromResult(false);
}

[Test]
Expand All @@ -40,7 +40,7 @@ public void GetIdenticalVernToWordTest()
_frontier.ElementAt(1).Vernacular = vern;
_frontier.ElementAt(2).Vernacular = vern;
_frontier.ElementAt(5).Vernacular = vern;
var wordLists = _dupFinder.GetIdenticalVernWords(_frontier, _isInBlacklist).Result;
var wordLists = _dupFinder.GetIdenticalVernWords(_frontier, _isUnavailableSet).Result;
Assert.That(wordLists, Has.Count.EqualTo(1));
Assert.That(wordLists.First(), Has.Count.EqualTo(3));
}
Expand All @@ -50,7 +50,7 @@ public void GetSimilarWordsAndMaxInListAndMaxListsTest()
{
_frontier = Util.RandomWordList(MaxInList * MaxLists, ProjId);
_dupFinder = new DuplicateFinder(MaxInList, MaxLists, NoMaxScore);
var wordLists = _dupFinder.GetSimilarWords(_frontier, _isInBlacklist).Result;
var wordLists = _dupFinder.GetSimilarWords(_frontier, _isUnavailableSet).Result;
Assert.That(wordLists, Has.Count.EqualTo(MaxLists));
Assert.That(wordLists.First(), Has.Count.EqualTo(MaxInList));
Assert.That(wordLists.Last(), Has.Count.EqualTo(MaxInList));
Expand All @@ -63,7 +63,7 @@ public void GetSimilarWordsAndMaxScoreTest()
// Ensure at least one set of similar words, in case MaxScore is too low.
_frontier.Last().Vernacular = _frontier.First().Vernacular;

var wordLists = _dupFinder.GetSimilarWords(_frontier, _isInBlacklist).Result;
var wordLists = _dupFinder.GetSimilarWords(_frontier, _isUnavailableSet).Result;
var firstList = wordLists.First();
var firstMin = _dupFinder.GetWordScore(firstList.First(), firstList.ElementAt(1));
var firstMax = _dupFinder.GetWordScore(firstList.First(), firstList.Last());
Expand All @@ -87,13 +87,13 @@ public void GetSimilarWordsAndMaxScoreTest()
}

[Test]
public void GetSimilarWordsBlacklistTest()
public void GetSimilarWordsBlacklistOrGraylistTest()
{
_frontier = Util.RandomWordList(MaxInList + 1, ProjId);
// Make sure the first set only is blacklisted, so all but the first word end up in a lone list.
_isInBlacklist = wordList => Task.FromResult(wordList.First() == _frontier.First().Vernacular);
// Make sure the first set only is black/gray-listed, so all but the first word end up in a lone list.
_isUnavailableSet = wordList => Task.FromResult(wordList.First() == _frontier.First().Vernacular);
_dupFinder = new DuplicateFinder(MaxInList, MaxLists, NoMaxScore);
var wordLists = _dupFinder.GetSimilarWords(_frontier, _isInBlacklist).Result;
var wordLists = _dupFinder.GetSimilarWords(_frontier, _isUnavailableSet).Result;
Assert.That(wordLists, Has.Count.EqualTo(1));
Assert.That(wordLists.First(), Has.Count.EqualTo(MaxInList));
}
Expand Down
Loading

0 comments on commit 2712a5d

Please sign in to comment.