|
| 1 | +using System.Threading.Tasks; |
| 2 | +using NGitLab.Models; |
| 3 | +using NGitLab.Tests.Docker; |
| 4 | +using NUnit.Framework; |
| 5 | + |
| 6 | +namespace NGitLab.Tests |
| 7 | +{ |
| 8 | + public class ProtectedBranchTests |
| 9 | + { |
| 10 | + [Test] |
| 11 | + public async Task ProtectBranch_Test() |
| 12 | + { |
| 13 | + using var context = await GitLabTestContext.CreateAsync(); |
| 14 | + var project = context.CreateProject(initializeWithCommits: true); |
| 15 | + var branchClient = context.Client.GetRepository(project.Id).Branches; |
| 16 | + var branch = branchClient.Create(new BranchCreate() { Name = "protectedBranch", Ref = project.DefaultBranch }); |
| 17 | + var protectedBranchClient = context.Client.GetProtectedBranchClient(project.Id); |
| 18 | + var branchProtect = new BranchProtect(branch.Name) |
| 19 | + { |
| 20 | + PushAccessLevel = AccessLevel.Maintainer, |
| 21 | + MergeAccessLevel = AccessLevel.NoAccess, |
| 22 | + AllowForcePush = true, |
| 23 | + AllowedToPush = new AccessLevelInfo[] |
| 24 | + { |
| 25 | + new AccessLevelInfo() |
| 26 | + { |
| 27 | + AccessLevel = AccessLevel.Admin, |
| 28 | + Description = "Admin", |
| 29 | + }, |
| 30 | + }, |
| 31 | + AllowedToUnprotect = new AccessLevelInfo[] |
| 32 | + { |
| 33 | + new AccessLevelInfo() |
| 34 | + { |
| 35 | + AccessLevel = AccessLevel.NoAccess, |
| 36 | + Description = "Example", |
| 37 | + }, |
| 38 | + }, |
| 39 | + }; |
| 40 | + |
| 41 | + // Protect branch |
| 42 | + ProtectedBranchAndBranchProtectAreEquals(branchProtect, protectedBranchClient.ProtectBranch(branchProtect)); |
| 43 | + |
| 44 | + // Get branch |
| 45 | + ProtectedBranchAndBranchProtectAreEquals(branchProtect, protectedBranchClient.GetProtectedBranch(branch.Name)); |
| 46 | + |
| 47 | + // Get branches |
| 48 | + Assert.IsNotEmpty(protectedBranchClient.GetProtectedBranches()); |
| 49 | + var protectedBranches = protectedBranchClient.GetProtectedBranches(branch.Name); |
| 50 | + Assert.IsNotEmpty(protectedBranches); |
| 51 | + ProtectedBranchAndBranchProtectAreEquals(branchProtect, protectedBranches[0]); |
| 52 | + |
| 53 | + // Unprotect branch |
| 54 | + protectedBranchClient.UnprotectBranch(branch.Name); |
| 55 | + Assert.IsEmpty(protectedBranchClient.GetProtectedBranches(branch.Name)); |
| 56 | + } |
| 57 | + |
| 58 | + private void ProtectedBranchAndBranchProtectAreEquals(BranchProtect branchProtect, ProtectedBranch protectedBranch) |
| 59 | + { |
| 60 | + Assert.AreEqual(branchProtect.BranchName, protectedBranch.Name); |
| 61 | + Assert.AreEqual(branchProtect.PushAccessLevel, protectedBranch.PushAccessLevels[0].AccessLevel); |
| 62 | + Assert.AreEqual(branchProtect.MergeAccessLevel, protectedBranch.MergeAccessLevels[0].AccessLevel); |
| 63 | + Assert.AreEqual(branchProtect.AllowForcePush, protectedBranch.AllowForcePush); |
| 64 | + } |
| 65 | + } |
| 66 | +} |
0 commit comments