mirror of
https://github.com/zoriya/octokit.net.git
synced 2026-06-06 20:13:40 +00:00
Add integration tests for Get, Update, Delete BranchProtection methods
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Octokit;
|
||||
using Octokit.Tests.Integration;
|
||||
@@ -23,6 +25,9 @@ public class RepositoryBranchesClientTests
|
||||
{
|
||||
Assert.NotNull(branch.Protection);
|
||||
}
|
||||
|
||||
// Ensure Protected attribute is deserialized (at least master branch should be true)
|
||||
Assert.True(branches.Any(x => x.Protected));
|
||||
}
|
||||
|
||||
[IntegrationTest]
|
||||
@@ -33,6 +38,15 @@ public class RepositoryBranchesClientTests
|
||||
var branches = await github.Repository.Branch.GetAll(7528679);
|
||||
|
||||
Assert.NotEmpty(branches);
|
||||
|
||||
// Ensure Protection attribute is deserialized
|
||||
foreach (var branch in branches)
|
||||
{
|
||||
Assert.NotNull(branch.Protection);
|
||||
}
|
||||
|
||||
// Ensure Protected attribute is deserialized (at least master branch should be true)
|
||||
Assert.True(branches.Any(x => x.Protected));
|
||||
}
|
||||
|
||||
[IntegrationTest]
|
||||
@@ -179,6 +193,12 @@ public class RepositoryBranchesClientTests
|
||||
|
||||
Assert.NotNull(branch);
|
||||
Assert.Equal("master", branch.Name);
|
||||
|
||||
// Ensure Protection attribute is deserialized
|
||||
Assert.NotNull(branch.Protection);
|
||||
|
||||
// Ensure Protected attribute is deserialized
|
||||
Assert.True(branch.Protected);
|
||||
}
|
||||
|
||||
[IntegrationTest]
|
||||
@@ -190,6 +210,12 @@ public class RepositoryBranchesClientTests
|
||||
|
||||
Assert.NotNull(branch);
|
||||
Assert.Equal("master", branch.Name);
|
||||
|
||||
// Ensure Protection attribute is deserialized
|
||||
Assert.NotNull(branch.Protection);
|
||||
|
||||
// Ensure Protected attribute is deserialized
|
||||
Assert.True(branch.Protected);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -290,4 +316,266 @@ public class RepositoryBranchesClientTests
|
||||
Assert.Equal(branch.Protection.RequiredStatusChecks.Contexts.Count, 0);
|
||||
}
|
||||
}
|
||||
|
||||
public class TheGetBranchProtectionMethod : IDisposable
|
||||
{
|
||||
IRepositoryBranchesClient _client;
|
||||
RepositoryContext _userRepoContext;
|
||||
OrganizationRepositoryWithTeamContext _orgRepoContext;
|
||||
|
||||
public TheGetBranchProtectionMethod()
|
||||
{
|
||||
var github = Helper.GetAuthenticatedClient();
|
||||
_client = github.Repository.Branch;
|
||||
|
||||
_userRepoContext = github.CreateRepositoryWithProtectedBranch().Result;
|
||||
_orgRepoContext = github.CreateOrganizationRepositoryWithProtectedBranch().Result;
|
||||
}
|
||||
|
||||
[IntegrationTest]
|
||||
public async Task GetsBranchProtection()
|
||||
{
|
||||
var repoOwner = _userRepoContext.RepositoryOwner;
|
||||
var repoName = _userRepoContext.RepositoryName;
|
||||
var protection = await _client.GetBranchProtection(repoOwner, repoName, "master");
|
||||
|
||||
Assert.NotNull(protection);
|
||||
Assert.NotNull(protection.RequiredStatusChecks);
|
||||
Assert.NotNull(protection.RequiredStatusChecks.Contexts);
|
||||
Assert.True(protection.RequiredStatusChecks.IncludeAdmins);
|
||||
Assert.True(protection.RequiredStatusChecks.Strict);
|
||||
Assert.Equal(2, protection.RequiredStatusChecks.Contexts.Count);
|
||||
|
||||
Assert.Null(protection.Restrictions);
|
||||
}
|
||||
|
||||
[IntegrationTest]
|
||||
public async Task GetsBranchProtectionWithRepositoryId()
|
||||
{
|
||||
var repoId = _userRepoContext.RepositoryId;
|
||||
var protection = await _client.GetBranchProtection(repoId, "master");
|
||||
|
||||
Assert.NotNull(protection);
|
||||
Assert.NotNull(protection.RequiredStatusChecks);
|
||||
Assert.NotNull(protection.RequiredStatusChecks.Contexts);
|
||||
Assert.True(protection.RequiredStatusChecks.IncludeAdmins);
|
||||
Assert.True(protection.RequiredStatusChecks.Strict);
|
||||
Assert.Equal(2, protection.RequiredStatusChecks.Contexts.Count);
|
||||
|
||||
Assert.Null(protection.Restrictions);
|
||||
}
|
||||
|
||||
[IntegrationTest]
|
||||
public async Task GetsBranchProtectionForOrgRepo()
|
||||
{
|
||||
var repoOwner = _orgRepoContext.RepositoryContext.RepositoryOwner;
|
||||
var repoName = _orgRepoContext.RepositoryContext.RepositoryName;
|
||||
var protection = await _client.GetBranchProtection(repoOwner, repoName, "master");
|
||||
|
||||
Assert.NotNull(protection);
|
||||
Assert.NotNull(protection.RequiredStatusChecks);
|
||||
Assert.NotNull(protection.RequiredStatusChecks.Contexts);
|
||||
Assert.True(protection.RequiredStatusChecks.IncludeAdmins);
|
||||
Assert.True(protection.RequiredStatusChecks.Strict);
|
||||
Assert.Equal(2, protection.RequiredStatusChecks.Contexts.Count);
|
||||
|
||||
Assert.NotNull(protection.Restrictions);
|
||||
Assert.Equal(1, protection.Restrictions.Teams.Count);
|
||||
Assert.Equal(0, protection.Restrictions.Users.Count);
|
||||
}
|
||||
|
||||
[IntegrationTest]
|
||||
public async Task GetsBranchProtectionForOrgRepoWithRepositoryId()
|
||||
{
|
||||
var repoId = _orgRepoContext.RepositoryContext.RepositoryId;
|
||||
var protection = await _client.GetBranchProtection(repoId, "master");
|
||||
|
||||
Assert.NotNull(protection);
|
||||
Assert.NotNull(protection.RequiredStatusChecks);
|
||||
Assert.NotNull(protection.RequiredStatusChecks.Contexts);
|
||||
Assert.True(protection.RequiredStatusChecks.IncludeAdmins);
|
||||
Assert.True(protection.RequiredStatusChecks.Strict);
|
||||
Assert.Equal(2, protection.RequiredStatusChecks.Contexts.Count);
|
||||
|
||||
Assert.NotNull(protection.Restrictions);
|
||||
Assert.Equal(1, protection.Restrictions.Teams.Count);
|
||||
Assert.Equal(0, protection.Restrictions.Users.Count);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_userRepoContext != null)
|
||||
_userRepoContext.Dispose();
|
||||
|
||||
if (_orgRepoContext != null)
|
||||
_orgRepoContext.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
public class TheUpdateBranchProtectionMethod : IDisposable
|
||||
{
|
||||
IRepositoryBranchesClient _client;
|
||||
RepositoryContext _userRepoContext;
|
||||
OrganizationRepositoryWithTeamContext _orgRepoContext;
|
||||
|
||||
public TheUpdateBranchProtectionMethod()
|
||||
{
|
||||
var github = Helper.GetAuthenticatedClient();
|
||||
_client = github.Repository.Branch;
|
||||
|
||||
_userRepoContext = github.CreateRepositoryWithProtectedBranch().Result;
|
||||
_orgRepoContext = github.CreateOrganizationRepositoryWithProtectedBranch().Result;
|
||||
}
|
||||
|
||||
[IntegrationTest]
|
||||
public async Task UpdatesBranchProtection()
|
||||
{
|
||||
var repoOwner = _userRepoContext.RepositoryOwner;
|
||||
var repoName = _userRepoContext.RepositoryName;
|
||||
var update = new BranchProtectionSettingsUpdate(
|
||||
new BranchProtectionRequiredStatusChecksUpdate(false, false, new[] { "new" }));
|
||||
|
||||
var protection = await _client.UpdateBranchProtection(repoOwner, repoName, "master", update);
|
||||
|
||||
Assert.NotNull(protection);
|
||||
Assert.NotNull(protection.RequiredStatusChecks);
|
||||
Assert.NotNull(protection.RequiredStatusChecks.Contexts);
|
||||
Assert.False(protection.RequiredStatusChecks.IncludeAdmins);
|
||||
Assert.False(protection.RequiredStatusChecks.Strict);
|
||||
Assert.Equal(1, protection.RequiredStatusChecks.Contexts.Count);
|
||||
|
||||
Assert.Null(protection.Restrictions);
|
||||
}
|
||||
|
||||
[IntegrationTest]
|
||||
public async Task UpdatesBranchProtectionWithRepositoryId()
|
||||
{
|
||||
var repoId = _userRepoContext.RepositoryId;
|
||||
var update = new BranchProtectionSettingsUpdate(
|
||||
new BranchProtectionRequiredStatusChecksUpdate(false, false, new[] { "new" }));
|
||||
|
||||
var protection = await _client.UpdateBranchProtection(repoId, "master", update);
|
||||
|
||||
Assert.NotNull(protection);
|
||||
Assert.NotNull(protection.RequiredStatusChecks);
|
||||
Assert.NotNull(protection.RequiredStatusChecks.Contexts);
|
||||
Assert.False(protection.RequiredStatusChecks.IncludeAdmins);
|
||||
Assert.False(protection.RequiredStatusChecks.Strict);
|
||||
Assert.Equal(1, protection.RequiredStatusChecks.Contexts.Count);
|
||||
|
||||
Assert.Null(protection.Restrictions);
|
||||
}
|
||||
|
||||
[IntegrationTest]
|
||||
public async Task UpdatesBranchProtectionForOrgRepo()
|
||||
{
|
||||
var repoOwner = _orgRepoContext.RepositoryContext.RepositoryOwner;
|
||||
var repoName = _orgRepoContext.RepositoryContext.RepositoryName;
|
||||
var update = new BranchProtectionSettingsUpdate(
|
||||
new BranchProtectionRequiredStatusChecksUpdate(false, false, new[] { "new" }),
|
||||
null);
|
||||
|
||||
var protection = await _client.UpdateBranchProtection(repoOwner, repoName, "master", update);
|
||||
|
||||
Assert.NotNull(protection);
|
||||
Assert.NotNull(protection.RequiredStatusChecks);
|
||||
Assert.NotNull(protection.RequiredStatusChecks.Contexts);
|
||||
Assert.False(protection.RequiredStatusChecks.IncludeAdmins);
|
||||
Assert.False(protection.RequiredStatusChecks.Strict);
|
||||
Assert.Equal(1, protection.RequiredStatusChecks.Contexts.Count);
|
||||
|
||||
Assert.Null(protection.Restrictions);
|
||||
}
|
||||
|
||||
[IntegrationTest]
|
||||
public async Task UpdatesBranchProtectionForOrgRepoWithRepositoryId()
|
||||
{
|
||||
var repoId = _orgRepoContext.RepositoryContext.RepositoryId;
|
||||
var update = new BranchProtectionSettingsUpdate(
|
||||
new BranchProtectionRequiredStatusChecksUpdate(false, false, new[] { "new" }),
|
||||
null);
|
||||
|
||||
var protection = await _client.UpdateBranchProtection(repoId, "master", update);
|
||||
|
||||
Assert.NotNull(protection);
|
||||
Assert.NotNull(protection.RequiredStatusChecks);
|
||||
Assert.NotNull(protection.RequiredStatusChecks.Contexts);
|
||||
Assert.False(protection.RequiredStatusChecks.IncludeAdmins);
|
||||
Assert.False(protection.RequiredStatusChecks.Strict);
|
||||
Assert.Equal(1, protection.RequiredStatusChecks.Contexts.Count);
|
||||
|
||||
Assert.Null(protection.Restrictions);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_userRepoContext != null)
|
||||
_userRepoContext.Dispose();
|
||||
|
||||
if (_orgRepoContext != null)
|
||||
_orgRepoContext.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
public class TheDeleteBranchProtectionMethod
|
||||
{
|
||||
IGitHubClient _github;
|
||||
IRepositoryBranchesClient _client;
|
||||
|
||||
public TheDeleteBranchProtectionMethod()
|
||||
{
|
||||
_github = Helper.GetAuthenticatedClient();
|
||||
_client = _github.Repository.Branch;
|
||||
}
|
||||
|
||||
[IntegrationTest]
|
||||
public async Task DeletesBranchProtection()
|
||||
{
|
||||
using (var context = await _github.CreateRepositoryWithProtectedBranch())
|
||||
{
|
||||
var repoOwner = context.RepositoryOwner;
|
||||
var repoName = context.RepositoryName;
|
||||
var deleted = await _client.DeleteBranchProtection(repoOwner, repoName, "master");
|
||||
|
||||
Assert.True(deleted);
|
||||
}
|
||||
}
|
||||
|
||||
[IntegrationTest]
|
||||
public async Task DeletesBranchProtectionWithRepositoryId()
|
||||
{
|
||||
using (var context = await _github.CreateRepositoryWithProtectedBranch())
|
||||
{
|
||||
var repoId = context.RepositoryId;
|
||||
var deleted = await _client.DeleteBranchProtection(repoId, "master");
|
||||
|
||||
Assert.True(deleted);
|
||||
}
|
||||
}
|
||||
|
||||
[IntegrationTest]
|
||||
public async Task DeletesBranchProtectionForOrgRepo()
|
||||
{
|
||||
using (var context = await _github.CreateOrganizationRepositoryWithProtectedBranch())
|
||||
{
|
||||
var repoOwner = context.RepositoryContext.RepositoryOwner;
|
||||
var repoName = context.RepositoryContext.RepositoryName;
|
||||
var deleted = await _client.DeleteBranchProtection(repoOwner, repoName, "master");
|
||||
|
||||
Assert.True(deleted);
|
||||
}
|
||||
}
|
||||
|
||||
[IntegrationTest]
|
||||
public async Task DeletesBranchProtectionForOrgRepoWithRepositoryId()
|
||||
{
|
||||
using (var context = await _github.CreateOrganizationRepositoryWithProtectedBranch())
|
||||
{
|
||||
var repoId = context.RepositoryContext.RepositoryId;
|
||||
var deleted = await _client.DeleteBranchProtection(repoId, "master");
|
||||
|
||||
Assert.True(deleted);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Octokit.Tests.Integration.Helpers
|
||||
{
|
||||
internal class OrganizationRepositoryWithTeamContext : IDisposable
|
||||
{
|
||||
internal RepositoryContext RepositoryContext { get; set; }
|
||||
internal TeamContext TeamContext { get; set; }
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (RepositoryContext != null)
|
||||
RepositoryContext.Dispose();
|
||||
|
||||
if (TeamContext != null)
|
||||
TeamContext.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
internal static class RepositoryProtectedBranchHelperExtensions
|
||||
{
|
||||
internal async static Task<RepositoryContext> CreateRepositoryWithProtectedBranch(this IGitHubClient client)
|
||||
{
|
||||
// Create user owned repo
|
||||
var userRepo = new NewRepository(Helper.MakeNameWithTimestamp("protected-repo")) { AutoInit = true };
|
||||
var contextUserRepo = await client.CreateRepositoryContext(userRepo);
|
||||
|
||||
// Protect master branch
|
||||
var update = new BranchProtectionSettingsUpdate(
|
||||
new BranchProtectionRequiredStatusChecksUpdate(true, true, new[] { "build", "test" }));
|
||||
|
||||
await client.Repository.Branch.UpdateBranchProtection(contextUserRepo.RepositoryOwner, contextUserRepo.RepositoryName, "master", update);
|
||||
|
||||
return contextUserRepo;
|
||||
}
|
||||
|
||||
internal async static Task<OrganizationRepositoryWithTeamContext> CreateOrganizationRepositoryWithProtectedBranch(this IGitHubClient client)
|
||||
{
|
||||
// Create organization owned repo
|
||||
var orgRepo = new NewRepository(Helper.MakeNameWithTimestamp("protected-org-repo")) { AutoInit = true };
|
||||
var contextOrgRepo = await client.CreateRepositoryContext(Helper.Organization, orgRepo);
|
||||
|
||||
// Create team in org
|
||||
var contextOrgTeam = await client.CreateTeamContext(Helper.Organization, new NewTeam(Helper.MakeNameWithTimestamp("team")));
|
||||
|
||||
// Grant team push access to repo
|
||||
await client.Organization.Team.AddRepository(
|
||||
contextOrgTeam.TeamId,
|
||||
contextOrgRepo.RepositoryOwner,
|
||||
contextOrgRepo.RepositoryName,
|
||||
new RepositoryPermissionRequest(Permission.Push));
|
||||
|
||||
// Protect master branch
|
||||
var orgUpdate = new BranchProtectionSettingsUpdate(
|
||||
new BranchProtectionRequiredStatusChecksUpdate(true, true, new[] { "build", "test" }),
|
||||
new ProtectedBranchRestrictionsUpdate(new[] { contextOrgTeam.TeamName }, new string[0]));
|
||||
await client.Repository.Branch.UpdateBranchProtection(contextOrgRepo.RepositoryOwner, contextOrgRepo.RepositoryName, "master", orgUpdate);
|
||||
|
||||
return new OrganizationRepositoryWithTeamContext
|
||||
{
|
||||
RepositoryContext = contextOrgRepo,
|
||||
TeamContext = contextOrgTeam
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,11 +13,13 @@ namespace Octokit.Tests.Integration.Helpers
|
||||
{
|
||||
_connection = connection;
|
||||
Repository = repo;
|
||||
RepositoryId = repo.Id;
|
||||
RepositoryOwner = repo.Owner.Login;
|
||||
RepositoryName = repo.Name;
|
||||
}
|
||||
|
||||
private IConnection _connection;
|
||||
internal int RepositoryId { get; private set; }
|
||||
internal string RepositoryOwner { get; private set; }
|
||||
internal string RepositoryName { get; private set; }
|
||||
|
||||
|
||||
@@ -137,6 +137,7 @@
|
||||
<Compile Include="Helpers\PersonalAccessTokenTestAttribute.cs" />
|
||||
<Compile Include="Helpers\PaidAccountTestAttribute.cs" />
|
||||
<Compile Include="Helpers\ReferenceExtensionsTests.cs" />
|
||||
<Compile Include="Helpers\OrganizationRepositoryWithTeamContext.cs" />
|
||||
<Compile Include="Helpers\TeamContext.cs" />
|
||||
<Compile Include="Helpers\RepositoryContext.cs" />
|
||||
<Compile Include="Helpers\RepositorySetupHelper.cs" />
|
||||
|
||||
Reference in New Issue
Block a user