This repository has been archived by the owner on Sep 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 654
/
CodeBaseHqFacts.cs
114 lines (98 loc) · 7.43 KB
/
CodeBaseHqFacts.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using System.Web;
using Kudu.Core.SourceControl;
using Kudu.Services.ServiceHookHandlers;
using Moq;
using Newtonsoft.Json.Linq;
using Xunit;
namespace Kudu.Services.Test
{
public class CodeBaseHqFacts
{
[Fact]
public void CodeBaseHqHandlerIgnoresNonCodeBaseHqPayloads()
{
// Arrange
var httpRequest = new Mock<HttpRequestBase>();
var handler = new CodebaseHqHandler();
// Act
DeploymentInfo deploymentInfo;
DeployAction result = handler.TryParseDeploymentInfo(httpRequest.Object, payload: null, targetBranch: null, deploymentInfo: out deploymentInfo);
// Assert
Assert.Equal(DeployAction.UnknownPayload, result);
}
[Theory]
[InlineData("{ invalid: 'payload' }")]
[InlineData("{ ref: '' }")]
[InlineData(@"{ ""repository"":{ ""url"":""http://test.codebasehq.com/projects/test-repositories/repositories/git1/commit/840daf31f4f87cb5cafd295ef75de989095f415b"" }, ref: """" }")]
public void GitHubHandlerHandlerReturnsNoOpForMalformedPayloads(string payloadContent)
{
// Arrange
var httpRequest = GetRequest();
var handler = new CodebaseHqHandler();
JObject payload = JObject.Parse(payloadContent);
// Act
DeploymentInfo deploymentInfo;
DeployAction result = handler.TryParseDeploymentInfo(httpRequest, payload: payload, targetBranch: "master", deploymentInfo: out deploymentInfo);
// Assert
Assert.Equal(DeployAction.NoOp, result);
}
[Fact]
public void CodeBaseHqHandlerProcessesPayload()
{
// Arrange
string payloadContent = @"{""before"":""fc10b3aa5a9e39ac326489805bba5c577f04db85"",""after"":""840daf31f4f87cb5cafd295ef75de989095f415b"",""ref"":""refs/heads/master"",""repository"":{""name"":""Git Repo #1"",""url"":""http://test.codebasehq.com/projects/test-repositories/repositories/git1"",""clone_url"":""git@codebasehq.com:test/test-repositories/git1.git"",""clone_urls"":{""ssh"":""git@codebasehq.com:test/test-repositories/git1.git"",""git"":""git://codebasehq.com:test/test-repositories/git1.git"",""http"":""https://test.codebasehq.com/test-repositories/git1.git""},""project"":{""name"":""Test Repositories"",""url"":""http://test.codebasehq.com/projects/test-repositories"",""status"":""active""}},""user"":{""name"":""Dan Wentworth"",""username"":""dan"",""email"":""dan@atechmedia.com""},""commits"":[{""id"":""840daf31f4f87cb5cafd295ef75de989095f415b"",""message"":""Extra output for the rrrraaaagh"",""author"":{""name"":""Dan Wentworth"",""email"":""dan@atechmedia.com""},""timestamp"":""Mon, 18 Jul 2011 10:50:01 +0100"",""url"":""http://test.codebasehq.com/projects/test-repositories/repositories/git1/commit/840daf31f4f87cb5cafd295ef75de989095f415b""}]}";
var httpRequest = GetRequest();
var handler = new CodebaseHqHandler();
JObject payload = JObject.Parse(payloadContent);
// Act
DeploymentInfo deploymentInfo;
DeployAction result = handler.TryParseDeploymentInfo(httpRequest, payload: payload, targetBranch: "master", deploymentInfo: out deploymentInfo);
// Assert
Assert.Equal(DeployAction.ProcessDeployment, result);
Assert.NotNull(deploymentInfo);
Assert.Equal("CodebaseHQ", deploymentInfo.Deployer);
Assert.Equal(RepositoryType.Git, deploymentInfo.RepositoryType);
Assert.Equal("https://test.codebasehq.com/test-repositories/git1.git", deploymentInfo.RepositoryUrl);
Assert.Equal("840daf31f4f87cb5cafd295ef75de989095f415b", deploymentInfo.TargetChangeset.Id);
Assert.Equal("Dan Wentworth", deploymentInfo.TargetChangeset.AuthorName);
Assert.Equal("dan@atechmedia.com", deploymentInfo.TargetChangeset.AuthorEmail);
Assert.Equal("Extra output for the rrrraaaagh", deploymentInfo.TargetChangeset.Message);
}
[Fact]
public void CodeBaseHqHandlerReturnsNoOpForDelete()
{
// Verifies delete scenario.
// Arrange
string payloadContent = @"{""before"":""fc10b3aa5a9e39ac326489805bba5c577f04db85"",""after"":""000000000000000000000000000000000"",""ref"":""refs/heads/master"",""repository"":{""name"":""Git Repo #1"",""url"":""http://test.codebasehq.com/projects/test-repositories/repositories/git1"",""clone_url"":""git@codebasehq.com:test/test-repositories/git1.git"",""clone_urls"":{""ssh"":""git@codebasehq.com:test/test-repositories/git1.git"",""git"":""git://codebasehq.com:test/test-repositories/git1.git"",""http"":""https://test.codebasehq.com/test-repositories/git1.git""},""project"":{""name"":""Test Repositories"",""url"":""http://test.codebasehq.com/projects/test-repositories"",""status"":""active""}},""user"":{""name"":""Dan Wentworth"",""username"":""dan"",""email"":""dan@atechmedia.com""},""commits"":[]}";
var httpRequest = GetRequest();
var handler = new CodebaseHqHandler();
JObject payload = JObject.Parse(payloadContent);
// Act
DeploymentInfo deploymentInfo;
DeployAction result = handler.TryParseDeploymentInfo(httpRequest, payload: payload, targetBranch: "master", deploymentInfo: out deploymentInfo);
// Assert
Assert.Equal(DeployAction.NoOp, result);
}
[Fact]
public void CodeBaseHqHandlerReturnsNoOpForDeploymentsToNonTargetBranch()
{
// Arrange
string payloadContent = @"{""before"":""fc10b3aa5a9e39ac326489805bba5c577f04db85"",""after"":""840daf31f4f87cb5cafd295ef75de989095f415b"",""ref"":""refs/heads/master"",""repository"":{""name"":""Git Repo #1"",""url"":""http://test.codebasehq.com/projects/test-repositories/repositories/git1"",""clone_url"":""git@codebasehq.com:test/test-repositories/git1.git"",""clone_urls"":{""ssh"":""git@codebasehq.com:test/test-repositories/git1.git"",""git"":""git://codebasehq.com:test/test-repositories/git1.git"",""http"":""https://test.codebasehq.com/test-repositories/git1.git""},""project"":{""name"":""Test Repositories"",""url"":""http://test.codebasehq.com/projects/test-repositories"",""status"":""active""}},""user"":{""name"":""Dan Wentworth"",""username"":""dan"",""email"":""dan@atechmedia.com""},""commits"":[{""id"":""840daf31f4f87cb5cafd295ef75de989095f415b"",""message"":""Extra output for the rrrraaaagh"",""author"":{""name"":""Dan Wentworth"",""email"":""dan@atechmedia.com""},""timestamp"":""Mon, 18 Jul 2011 10:50:01 +0100"",""url"":""http://test.codebasehq.com/projects/test-repositories/repositories/git1/commit/840daf31f4f87cb5cafd295ef75de989095f415b""}]}";
var httpRequest = GetRequest();
var handler = new CodebaseHqHandler();
JObject payload = JObject.Parse(payloadContent);
// Act
DeploymentInfo deploymentInfo;
DeployAction result = handler.TryParseDeploymentInfo(httpRequest, payload: payload, targetBranch: "production", deploymentInfo: out deploymentInfo);
// Assert
Assert.Equal(DeployAction.NoOp, result);
Assert.Null(deploymentInfo);
}
private static HttpRequestBase GetRequest()
{
var httpRequest = new Mock<HttpRequestBase>();
httpRequest.SetupGet(r => r.UserAgent).Returns("Codebasehq.com");
return httpRequest.Object;
}
}
}