Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions SS14.Labeller.Tests/CustomWebApplicationFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ namespace SS14.Labeller.Tests;
[ExcludeFromCodeCoverage]
public class CustomWebApplicationFactory : WebApplicationFactory<Program>
{
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.
public IGitHubApiClient GitHubApiClient { get; private set; }
public IDiscourseClient DiscourseClient { get; private set; }
public IDiscourseTopicsRepository TopicsRepository { get; private set; }
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.

/// <inheritdoc />
protected override void ConfigureWebHost(IWebHostBuilder builder)
Expand All @@ -46,6 +48,7 @@ protected override void ConfigureWebHost(IWebHostBuilder builder)
{
{ "Discourse:ApiKey", "wawa" },
{ "Discourse:Username", "aw" },
{ "Discourse:Enable", "true" },
{ "Discourse:DiscussionCategoryId", "42" },
{ "Discourse:Url", "http://wa.wa" },
{ "GitHub:WebhookSecret", IntegrationTests.HookSecret },
Expand Down
16 changes: 8 additions & 8 deletions SS14.Labeller.Tests/GitHubApi/GithubRetryHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void Setup()
public void SendAsync_SuccessfulRequest()
{
// Arrange
_mockInnerHandler.Send(Arg.Any<HttpRequestMessage>(), Arg.Any<CancellationToken>())
_mockInnerHandler.SendStub(Arg.Any<HttpRequestMessage>(), Arg.Any<CancellationToken>())
.Returns(Task.FromResult(new HttpResponseMessage { StatusCode = HttpStatusCode.OK }));

var handler = new GithubRetryHandler(_mockInnerHandler, _config, _logger);
Expand All @@ -46,14 +46,14 @@ public void SendAsync_SuccessfulRequest()
var result = httpClient.SendAsync(_httpRequestMessage, default).Result;

// Assert
Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.OK));
}

[Test]
public void SendAsync_NetworkErrorRetries()
{
// Arrange
_mockInnerHandler.Send(Arg.Any<HttpRequestMessage>(), Arg.Any<CancellationToken>())
_mockInnerHandler.SendStub(Arg.Any<HttpRequestMessage>(), Arg.Any<CancellationToken>())
.Returns(
_=> throw new HttpRequestException(HttpRequestError.ConnectionError),
_=> Task.FromResult(new HttpResponseMessage { StatusCode = HttpStatusCode.OK })
Expand All @@ -66,7 +66,7 @@ public void SendAsync_NetworkErrorRetries()
var result = httpClient.SendAsync(_httpRequestMessage, default).Result;

// Assert
Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.OK));
}

[Test]
Expand All @@ -84,7 +84,7 @@ public void SendAsync_CalculateNextRequestTimeWithRateLimits()

var response2 = new HttpResponseMessage(HttpStatusCode.OK);

_mockInnerHandler.Send(Arg.Any<HttpRequestMessage>(), Arg.Any<CancellationToken>())
_mockInnerHandler.SendStub(Arg.Any<HttpRequestMessage>(), Arg.Any<CancellationToken>())
.Returns(response1, response2);

var handler = new GithubRetryHandler(_mockInnerHandler, _config, _logger);
Expand All @@ -103,7 +103,7 @@ public void SendAsync_CalculateNextRequestTimeWithRateLimits()
public void SendAsync_MaxRetryExceeded()
{
// Arrange
_mockInnerHandler.Send(Arg.Any<HttpRequestMessage>(), Arg.Any<CancellationToken>())
_mockInnerHandler.SendStub(Arg.Any<HttpRequestMessage>(), Arg.Any<CancellationToken>())
.ThrowsAsync(new HttpRequestException(HttpRequestError.ConnectionError));

_gitHubConfig.MaxRetryAttempt = 2;
Expand All @@ -125,10 +125,10 @@ public class MockHttpMessageHandler : HttpMessageHandler
{
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
return Send(request, cancellationToken);
return SendStub(request, cancellationToken);
}

public virtual Task<HttpResponseMessage> Send(HttpRequestMessage request, CancellationToken cancellationToken)
public virtual Task<HttpResponseMessage> SendStub(HttpRequestMessage request, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
Expand Down
41 changes: 41 additions & 0 deletions SS14.Labeller.Tests/IntegrationTests.PullRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -310,4 +310,45 @@ await _applicationFactory.GitHubApiClient
Arg.Any<CancellationToken>()
);
}

[Test]
public async Task PullRequest_TaggedWithNeedsDiscussionAndNoExistingDiscussion_NewDiscourseThreadSaved()
{
// Arrange
const string fileName = "pull_request_needs_discussion.json";
var requestContent = await CreateRequestContent(fileName, "pull_request");
_applicationFactory.TopicsRepository
.HasTopic("Fildrance", "SS14.Labeller", 36, Arg.Any<CancellationToken>())
.Returns(false);

_applicationFactory.DiscourseClient.CreateTopic(42, Arg.Any<string>(), Arg.Any<string>(), Arg.Any<CancellationToken>())
.Returns(Task.FromResult(new DiscourseCreatedPost{PostUrl = "https://discourse.example.com/t/42", TopicId = 43}));

// Act
await _client.PostAsync("/webhook", requestContent);

// Assert
await _applicationFactory.TopicsRepository
.Received()
.Add("Fildrance", "SS14.Labeller", 36, 43, Arg.Any<CancellationToken>());
}

[Test]
public async Task PullRequest_TaggedWithNeedsDiscussionAndExistingDiscussion_NoNewDiscussionCreated()
{
// Arrange
const string fileName = "pull_request_needs_discussion.json";
var requestContent = await CreateRequestContent(fileName, "pull_request");
_applicationFactory.TopicsRepository
.HasTopic("Fildrance", "SS14.Labeller", 36, Arg.Any<CancellationToken>())
.Returns(true);


// Act
await _client.PostAsync("/webhook", requestContent);

// Assert
await _applicationFactory.DiscourseClient.DidNotReceive()
.CreateTopic(Arg.Any<int>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<CancellationToken>());
}
}
Loading
Loading