void ValidateAddOrRemoveDataset(Sandbox sandbox) { var sandboxPhase = SandboxPhaseUtil.GetCurrentPhase(sandbox); if (sandboxPhase > SandboxPhase.Open) { throw new ArgumentException($"Dataset cannot be added to Sandbox. Sandbox phase must be open."); } }
public void CallingCurrentPhase_WithEmptyPhaseList_WillReturnOpen() { var sandboxWithoutPhase = new Sandbox() { PhaseHistory = new List <SandboxPhaseHistory>() { } }; var phaseOfSandbox = SandboxPhaseUtil.GetCurrentPhase(sandboxWithoutPhase); Assert.Equal(SandboxPhase.Open, phaseOfSandbox); }
public void CallingCurrentPhase_WithLastPhaseBeingOpen_WillReturnOpen() { var sandboxWithoutPhase = new Sandbox() { PhaseHistory = new List <SandboxPhaseHistory>() { new SandboxPhaseHistory() { Counter = 0, Phase = SandboxPhase.Open } } }; var phaseOfSandbox = SandboxPhaseUtil.GetCurrentPhase(sandboxWithoutPhase); Assert.Equal(SandboxPhase.Open, phaseOfSandbox); }
public SandboxPhase Resolve(Sandbox source, IHasCurrentPhase destination, SandboxPhase destMember, ResolutionContext context) { return(SandboxPhaseUtil.GetCurrentPhase(source)); }
async Task ValidateRuleUpdateInputThrowIfNot(CloudResource vm, List <VmRuleDto> existingRules, List <VmRuleDto> updatedRuleSet) { var validationErrors = new List <string>(); var sandbox = await _db.Sandboxes.Include(sb => sb.PhaseHistory).FirstOrDefaultAsync(sb => sb.Id == vm.SandboxId); var curPhase = SandboxPhaseUtil.GetCurrentPhase(sandbox); //VALIDATE OUTBOUND RULE, THERE SHOULD BE ONLY ONE var outboundRules = updatedRuleSet.Where(r => r.Direction == RuleDirection.Outbound).ToList(); if (outboundRules.Count != 1) { validationErrors.Add($"Multiple outbound rule(s) provided"); ValidationUtils.ThrowIfValidationErrors("Rule update not allowed", validationErrors); } var onlyOutboundRuleFromExisting = existingRules.SingleOrDefault(r => r.Direction == RuleDirection.Outbound); var onlyOutboundRuleFromClient = outboundRules.SingleOrDefault(); if (onlyOutboundRuleFromExisting.Name != onlyOutboundRuleFromClient.Name) { validationErrors.Add($"Illegal outbound rule(s) provided"); ValidationUtils.ThrowIfValidationErrors("Rule update not allowed", validationErrors); } //If Sandbox is not open, make sure outbound rule has not changed if (curPhase > SandboxPhase.Open) { if (onlyOutboundRuleFromClient.Direction == RuleDirection.Outbound) { if (onlyOutboundRuleFromClient.ToString() != onlyOutboundRuleFromExisting.ToString()) { var currentUser = await _userService.GetCurrentUserAsync(); if (!currentUser.Admin) { validationErrors.Add($"Only admin can updated outgoing rules when Sandbox is in phase {curPhase}"); ValidationUtils.ThrowIfValidationErrors("Rule update not allowed", validationErrors); } } } } //VALIDATE INBOUND RULES foreach (var curInboundRule in updatedRuleSet.Where(r => r.Direction == RuleDirection.Inbound).ToList()) { if (curInboundRule.Direction > RuleDirection.Outbound) { validationErrors.Add($"Invalid direction for rule {curInboundRule.Description}: {curInboundRule.Direction}"); } if (String.IsNullOrWhiteSpace(curInboundRule.Ip)) { validationErrors.Add($"Missing ip for rule {curInboundRule.Description}"); } if (curInboundRule.Port <= 0) { validationErrors.Add($"Invalid port for rule {curInboundRule.Description}: {curInboundRule.Port}"); } if (String.IsNullOrWhiteSpace(curInboundRule.Description)) { validationErrors.Add($"Missing Description for rule {curInboundRule.Description}"); } } ValidationUtils.ThrowIfValidationErrors("Rule update not allowed", validationErrors); }
public void CallingCurrentPhase_WithPhaseListBeingNull_WillThrow() { var sandboxWithoutPhase = new Sandbox(); Assert.Throws <Exception>(() => SandboxPhaseUtil.GetCurrentPhase(sandboxWithoutPhase)); }
public void CallingCurrentPhase_WithNullArgument_WillThrow() { Assert.Throws <ArgumentNullException>(() => SandboxPhaseUtil.GetCurrentPhase(null)); }