private void SetupMock(Guid oldRevisionId, bool addIpmMember, bool addRequisitioner, bool addBrandMember, Guid costStageRevisionId, Guid costId, Builders.Response.ApprovalModel approvalModels)
            {
                var oldRevision = SetupCostStageRevision(oldRevisionId, costId);

                var existingApprovals = CreateExistingApprovals(oldRevision, addIpmMember, addRequisitioner, addBrandMember);

                var updateApprovalsModel = CreateUpdateApprovalsModel();

                EFContext.Approval.AddRange(existingApprovals);
                EFContext.CostUser.AddRange(updateApprovalsModel.SelectMany(am => am.ApprovalMembers).Select(m => new CostUser {
                    Id = m.Id
                }));

                EFContext.Role.Add(new Role {
                    Id = Guid.NewGuid(), Name = "cost.approver"
                });

                var revision = SetupCostStageRevision(costStageRevisionId, costId);

                EFContext.CostStageRevision.AddRange(revision, oldRevision);

                CostBuilderMock.Setup(s => s.GetApprovals(It.IsAny <CostType>(), It.IsAny <IStageDetails>(), It.IsAny <Guid>(), It.IsAny <Guid>(), It.IsAny <Guid>()))
                .ReturnsAsync(new List <Builders.Response.ApprovalModel> {
                    approvalModels
                });

                EFContext.SaveChanges();
            }
Exemple #2
0
            public async Task IsValidForSubmission_When_Invalid_Should_SendNotificationToSupportTeam()
            {
                // Arrange
                var          costId     = Guid.NewGuid();
                const string message    = "Any message";
                const string costNumber = "PO12312313";

                EFContext.Add(new Cost
                {
                    Id         = costId,
                    CostNumber = costNumber
                });
                EFContext.SaveChanges();

                CostBuilderMock.Setup(cb => cb.IsValidForSubmittion(costId)).ReturnsAsync(new OperationResponse(false, message));

                // Act
                var result = await CostService.IsValidForSubmission(User, costId);

                // Assert
                result.Should().NotBeNull();
                result.Success.Should().Be(false);
                SupportNotificationServiceMock.Verify(sn =>
                                                      sn.SendSupportSubmissionFailedNotification(costNumber, null), Times.Once);
            }
            private CostStageRevision SetupCostStageRevision(Guid costStageRevisionId, Guid costId, bool mockApproval = false)
            {
                var costStageId = Guid.NewGuid();
                var revision    = new CostStageRevision
                {
                    Id           = costStageRevisionId,
                    StageDetails = new CustomFormData
                    {
                        Id   = new Guid(),
                        Data = JsonConvert.SerializeObject(new Dictionary <string, dynamic>())
                    },
                    CostStageId = costStageId,
                    CostStage   = new CostStage
                    {
                        Id = costStageId,

                        CostId = costId,
                        Cost   = new Cost
                        {
                            Id       = costId,
                            CostType = CostType.Buyout,
                        }
                    }
                };

                if (mockApproval)
                {
                    CostBuilderMock.Setup(s => s.GetApprovals(It.IsAny <CostType>(), It.IsAny <IStageDetails>(), It.IsAny <Guid>(), It.IsAny <Guid>(), It.IsAny <Guid>()))
                    .ReturnsAsync(new List <Builders.Response.ApprovalModel>());
                }
                return(revision);
            }
Exemple #4
0
            public async Task InsertCost()
            {
                //Setup
                var adCostNumber = "adCostNumber";
                var templateId   = Guid.NewGuid();
                var requestModel = CreateCostModel(templateId);
                var costModel    = new CostBuilderModel
                {
                    Stages = new[]
                    {
                        new CostStageModel
                        {
                            Revisions = new []
                            {
                                new CostStageRevisionModel
                                {
                                    SupportingDocuments = new List <SupportingDocumentModel>()
                                }
                            }
                        }
                    }
                };

                var costUser = new CostUser
                {
                    Id     = User.Id,
                    Agency = new Agency
                    {
                        Id = User.AgencyId,
                    }
                };

                var responseMock = new Mock <ICreateCostResponse>();

                responseMock.SetupGet(f => f.Cost).Returns(costModel);
                PgUserServiceMock.Setup(a => a.GrantUsersAccess(It.IsAny <Cost>(), It.IsAny <CreateCostModel>())).Returns(Task.CompletedTask);
                CostBuilderMock.Setup(e => e.CreateCost(It.IsAny <CostUser>(), It.IsAny <CreateCostModel>()))
                .ReturnsAsync(responseMock.Object);

                EFContext.Project.Add(new Project {
                    Id = Guid.NewGuid(), AdCostNumber = adCostNumber
                });
                EFContext.CostUser.Add(costUser);
                await EFContext.SaveChangesAsync();

                PermissionService.Setup(a => a.CreateDomainNode(It.IsAny <string>(), It.IsAny <Guid>(), It.IsAny <Guid>(), It.IsAny <Guid>()))
                .ReturnsAsync(new[] { Guid.NewGuid().ToString() });
                //Act
                await CostService.CreateCost(User, requestModel);

                //Assert
                CostBuilderMock.Verify(e => e.CreateCost(costUser, requestModel), Times.Once);
                EFContext.Cost.Should().HaveCount(1);
                EFContext.NotificationSubscriber.Should().HaveCount(0);
                Mapper.Verify(m => m.Map <CostModel>(It.Is <Cost>(c => c != null && c.Status == CostStageRevisionStatus.Draft)));
            }
Exemple #5
0
            public async Task GrandEditPermissionToOwner()
            {
                //Setup
                var adCostNumber = "adCostNumber";
                var templateId   = Guid.NewGuid();
                var requestModel = CreateCostModel(templateId);
                var costModel    = new CostBuilderModel
                {
                    Stages = new[]
                    {
                        new CostStageModel
                        {
                            Revisions = new []
                            {
                                new CostStageRevisionModel
                                {
                                    SupportingDocuments = new List <SupportingDocumentModel>()
                                }
                            }
                        }
                    }
                };

                var costUser = new CostUser
                {
                    Id     = User.Id,
                    Agency = new Agency
                    {
                        Id = User.AgencyId,
                    }
                };

                var responseMock = new Mock <ICreateCostResponse>();

                responseMock.SetupGet(f => f.Cost).Returns(costModel);
                PgUserServiceMock.Setup(a => a.GrantUsersAccess(It.IsAny <Cost>(), It.IsAny <CreateCostModel>())).Returns(Task.CompletedTask);
                CostBuilderMock.Setup(e => e.CreateCost(It.IsAny <CostUser>(), It.IsAny <CreateCostModel>()))
                .ReturnsAsync(responseMock.Object);

                EFContext.Project.Add(new Project {
                    Id = Guid.NewGuid(), AdCostNumber = adCostNumber
                });
                EFContext.CostUser.Add(costUser);
                await EFContext.SaveChangesAsync();

                PermissionService.Setup(a => a.CreateDomainNode(It.IsAny <string>(), It.IsAny <Guid>(), It.IsAny <Guid>(), It.IsAny <Guid>()))
                .ReturnsAsync(new[] { Guid.NewGuid().ToString() });
                //Act
                await CostService.CreateCost(User, requestModel);

                //Assert
                CostStageRevisionPermissionServiceMock.Verify(p =>
                                                              p.GrantCostPermission(It.IsAny <Guid>(), Roles.CostEditor, It.Is <IEnumerable <CostUser> >(c => c.Any(i => i == costUser)), BuType.Pg, It.IsAny <Guid?>(), true),
                                                              Times.Once);
            }
Exemple #6
0
            public async Task ReturnFailureReponse_WhenCostIsNotValidForSubmittion()
            {
                // Arrange
                var cost = MockCost();

                CostBuilderMock.Setup(b => b.IsValidForSubmittion(cost.Id)).ReturnsAsync(new OperationResponse {
                    Success = false
                });

                // Act
                var response = await CostActionService.Submit(cost.Id, User);

                // Assert
                response.Success.Should().BeFalse();
            }
Exemple #7
0
            public async Task UpdateCostStatusAndSendForApproval()
            {
                // Arrange
                var cost = MockCost();

                CostBuilderMock.Setup(b => b.IsValidForSubmittion(cost.Id)).ReturnsAsync(new OperationResponse {
                    Success = true
                });

                // Act
                await CostActionService.Submit(cost.Id, User);

                // Assert
                ApprovalServiceMock.Verify(cs => cs.SubmitApprovals(It.IsAny <Cost>(), It.IsAny <CostUser>(), It.IsAny <IEnumerable <Approval> >(), It.IsAny <BuType>()), Times.Once);
                CostStatusServiceMock.Verify(cs => cs.UpdateCostStatus(User.BuType, cost.Id, CostAction.Submit), Times.Once);
            }
Exemple #8
0
            public async Task UpdateSubmittedAtOfTheCostStageRevision()
            {
                // Arrange
                var cost = MockCost();
                var timeJustBeforeSubmittion = DateTime.UtcNow;

                CostBuilderMock.Setup(b => b.IsValidForSubmittion(cost.Id)).ReturnsAsync(new OperationResponse {
                    Success = true
                });

                // Act
                await CostActionService.Submit(cost.Id, User);

                // Assert
                cost.LatestCostStageRevision.Submitted.Should().NotBeNull();
                Debug.Assert(cost.LatestCostStageRevision.Submitted != null, "cost.LatestCostStageRevision.Submitted != null");
                cost.LatestCostStageRevision.Submitted.Value.Should().BeOnOrAfter(timeJustBeforeSubmittion);
                cost.LatestCostStageRevision.Submitted.Value.Should().BeOnOrBefore(DateTime.UtcNow);
            }
Exemple #9
0
            public async Task IsValidForSubmission_Always_Should_GetResultFromCostBuilder(bool isValid)
            {
                // Arrange
                var          costId  = Guid.NewGuid();
                const string message = "Any message";

                EFContext.Add(new Cost {
                    Id = costId
                });
                EFContext.SaveChanges();

                CostBuilderMock.Setup(cb => cb.IsValidForSubmittion(costId)).ReturnsAsync(new OperationResponse(isValid, message));

                // Act
                var result = await CostService.IsValidForSubmission(User, costId);

                // Assert
                result.Should().NotBeNull();
                result.Success.Should().Be(isValid);
            }
Exemple #10
0
            public async Task UpdateCost_WhenSupportingDocumentsHaveChanged()
            {
                // Arrange
                var    costId          = Guid.NewGuid();
                var    stageRevisionId = Guid.NewGuid();
                var    documentId      = Guid.NewGuid();
                string data            = "{\"smoId\": null, \"title\": \"asdf\", \"isAIPE\": false, \"campaign\": \"15xStronger (Pantene)\", \"costType\": \"Production\", \"projectId\": \"5aa6958836d1a002c68a1424\", \"costNumber\": \"PGI0005D0000001\", \"contentType\": {\"id\": \"0cbdb306-f3bd-4d2e-a70c-6cfe3b3dc41b\", \"key\": \"Digital\", \"value\": \"Digital\", \"created\": \"2018-02-13T14:42:56.684202\", \"visible\": true, \"modified\": \"2018-02-13T14:42:56.684201\", \"projects\": null, \"createdById\": \"77681eb0-fc0d-44cf-83a0-36d51851e9ae\", \"dictionaryId\": \"a97b2773-66eb-4d07-9bc5-8af5bf784d53\"}, \"budgetRegion\": {\"id\": \"7291b9c2-92cd-488b-8c68-f03f339b3c18\", \"key\": \"EUROPE AREA\", \"name\": \"Europe\"}, \"organisation\": {\"id\": \"fe8f1631-0c15-44be-a5e5-16d42fb62d2f\", \"key\": \"BFO\", \"value\": \"BFO\", \"created\": \"2018-02-13T14:42:56.696786\", \"visible\": true, \"modified\": \"2018-02-13T14:42:56.696785\", \"projects\": null, \"createdById\": \"77681eb0-fc0d-44cf-83a0-36d51851e9ae\", \"dictionaryId\": \"f1ce7c45-c502-488f-b33c-15554b60c1f1\"}, \"initialBudget\": 50000, \"agencyCurrency\": \"USD\", \"agencyProducer\": [\"Abby Jenkins (Leo Burnett)\"], \"IsCurrencyChanged\": false}";
                var    cost            = new Cost
                {
                    Id    = costId,
                    Owner = new CostUser(),
                    LatestCostStageRevision = new CostStageRevision()
                    {
                        Id = stageRevisionId,
                        SupportingDocuments = new List <SupportingDocument>()
                        {
                            new SupportingDocument()
                            {
                                Id = documentId,
                                CostStageRevisionId = stageRevisionId,
                                Key  = "file1 key",
                                Name = "file1 name",
                            },
                            new SupportingDocument()
                            {
                                Id = Guid.NewGuid(),
                                CostStageRevisionId = stageRevisionId,
                            }
                        },
                        StageDetails = new CustomFormData()
                        {
                            Data = data,
                        },
                        ProductDetails = new CustomFormData()
                        {
                            Data = data,
                        },
                        CostStage = new CostStage()
                        {
                            Key  = String.Empty,
                            Name = String.Empty,
                        },
                    },
                };

                CostBuilderMock.Setup(cb => cb.UpdateCost(It.IsAny <UserIdentity>(), It.IsAny <Guid>(), It.IsAny <Guid>(), It.IsAny <CostType>(), It.IsAny <StageDetails>(), It.IsAny <ProductionDetail>()))
                .ReturnsAsync(new plugins.PG.Builders.Cost.UpdateCostResponse()
                {
                    StageDetails          = cost.LatestCostStageRevision.StageDetails,
                    ProductionDetails     = cost.LatestCostStageRevision.ProductDetails,
                    CurrentCostStageModel = new CostStageModel()
                    {
                        Key       = "FirstStage Key",
                        Name      = "FirstStage Name",
                        Order     = 1,
                        Revisions = new[]
                        {
                            new CostStageRevisionModel
                            {
                                Name                = "FirstStage Key",
                                Status              = CostStageRevisionStatus.Draft,
                                StageDetails        = data,
                                SupportingDocuments = new []
                                {
                                    new SupportingDocumentModel()
                                    {
                                        Key  = "file1 key",
                                        Name = "file1 name",
                                    },
                                    new SupportingDocumentModel(),
                                }
                            }
                        }
                    }
                });

                EFContext.Cost.Add(cost);
                EFContext.SaveChanges();
                cost.LatestCostStageRevision.SupportingDocuments.Add(new SupportingDocument());

                // Act
                await CostService.UpdateCost(costId, User, new UpdateCostModel());

                // Assert
                var documents = EFContext.SupportingDocument.Where(sd => sd.CostStageRevisionId == stageRevisionId).ToList();

                documents.Count.Should().Be(3);
                documents.Count(sd => sd.Id == documentId).Should().Be(1);
            }
Exemple #11
0
            public async Task InsertDependentItems()
            {
                // Arrange
                var adCostNumber = "adCostNumber";
                var templateId   = Guid.NewGuid();
                var requestModel = CreateCostModel(templateId);

                var supportingDocumentModel = new SupportingDocumentModel();
                var costStageRevisionModel  = new CostStageRevisionModel
                {
                    StageDetails        = @"{ ""abc"": ""abc"" }",
                    SupportingDocuments = new[] { supportingDocumentModel }
                };
                var costStageModel = new CostStageModel
                {
                    Revisions = new[] { costStageRevisionModel }
                };
                var costModel = new CostBuilderModel
                {
                    Stages = new[] { costStageModel }
                };
                var costUser = new CostUser
                {
                    Id     = User.Id,
                    Agency = new Agency
                    {
                        Id = User.AgencyId,
                    }
                };

                var responseMock = new Mock <ICreateCostResponse>();

                responseMock.SetupGet(f => f.Cost).Returns(costModel);
                CostBuilderMock.Setup(e => e.CreateCost(It.IsAny <CostUser>(), It.IsAny <CreateCostModel>()))
                .ReturnsAsync(responseMock.Object);

                EFContext.Project.Add(new Project {
                    Id = Guid.NewGuid(), AdCostNumber = adCostNumber
                });
                EFContext.CostUser.Add(costUser);
                await EFContext.SaveChangesAsync();

                // Act
                await CostService.CreateCost(User, requestModel);

                // Assert
                EFContext.Cost.Should().HaveCount(1);
                var cost = EFContext.Cost.First();

                EFContext.CostStage.Should().HaveCount(1);
                var costStage = EFContext.CostStage.First();

                costStage.CostId.Should().Be(cost.Id);

                EFContext.CostStageRevision.Should().HaveCount(1);
                var costStageRevision = EFContext.CostStageRevision.First();

                costStageRevision.CostStageId.Should().Be(costStage.Id);
                costStageRevision.StageDetails.Should().NotBeNull();
                costStageRevision.ProductDetails.Should().NotBeNull();
                costStageRevision.SupportingDocuments.Should().NotBeNull();
            }
            public async Task AddNewApprovalMembers()
            {
                // Arrange
                var costStageRevisionId = Guid.NewGuid();
                var oldRevisionId       = Guid.NewGuid();
                var costId            = Guid.NewGuid();
                var revision          = SetupCostStageRevision(costStageRevisionId, costId, true);
                var existingApprovals = CreateExistingApprovals(revision);

                var updateApprovalsModel = CreateUpdateApprovalsModel();

                EFContext.CostUser.AddRange(updateApprovalsModel.SelectMany(am => am.ApprovalMembers).Select(m => new CostUser
                {
                    Id = m.Id,
                    UserBusinessRoles = new List <UserBusinessRole>
                    {
                        new UserBusinessRole
                        {
                            BusinessRole = new BusinessRole
                            {
                                Key = Constants.BusinessRole.Ipm
                            }
                        },
                        new UserBusinessRole
                        {
                            BusinessRole = new BusinessRole
                            {
                                Key = Constants.BusinessRole.BrandManager
                            }
                        }
                    }
                }));
                EFContext.Approval.AddRange(existingApprovals);

                EFContext.Role.Add(new Role {
                    Id = Guid.NewGuid(), Name = "cost.approver"
                });

                var oldRevision = SetupCostStageRevision(oldRevisionId, costId, true);

                EFContext.CostStageRevision.AddRange(oldRevision);

                ApprovalServiceMock.Setup(a =>
                                          a.GetApprovalsByCostStageRevisionId(It.IsAny <Guid>(), false)
                                          )
                .ReturnsAsync(existingApprovals.Select(a => new Approval
                {
                    Id     = a.Id,
                    Type   = a.Type,
                    Status = a.Status,
                    CostStageRevisionId = a.CostStageRevisionId,
                    CreatedById         = a.CreatedById,
                    ApprovalMembers     = new List <ApprovalMember>(),
                    Requisitioners      = new List <Requisitioner>(),
                    Created             = a.Created,
                    Modified            = a.Modified,
                    ValidBusinessRoles  = a.ValidBusinessRoles
                }).ToList());

                EFContext.Role.Add(new Role {
                    Id = Guid.NewGuid(), Name = "cost.approver"
                });
                EFContext.SaveChanges();

                var ipmApprovalModel = new Builders.Response.ApprovalModel
                {
                    Type = ApprovalType.IPM,
                    ValidBusinessRoles = new [] { Constants.BusinessRole.Ipm }
                };
                var brandApprovalModel = new Builders.Response.ApprovalModel
                {
                    Type = ApprovalType.Brand,
                    ValidBusinessRoles = new [] { Constants.BusinessRole.BrandManager }
                };

                CostBuilderMock.Setup(b =>
                                      b.GetApprovals(It.IsAny <CostType>(), It.IsAny <IStageDetails>(), It.IsAny <Guid>(), costStageRevisionId, costId))
                .ReturnsAsync(new List <Builders.Response.ApprovalModel> {
                    ipmApprovalModel, brandApprovalModel
                });

                // Act
                await CostApprovalService.UpdateApprovals(costId, costStageRevisionId, User, updateApprovalsModel);

                // Assert
                EFContext.Entry(revision).Collection(r => r.Approvals).Load();

                var ipmApproval = await EFContext.Approval
                                  .Include(a => a.ApprovalMembers)
                                  .FirstAsync(a => a.CostStageRevisionId == costStageRevisionId && a.Type == ApprovalType.IPM);

                var brandApproval = await EFContext.Approval
                                    .Include(a => a.ApprovalMembers)
                                    .FirstAsync(a => a.CostStageRevisionId == costStageRevisionId && a.Type == ApprovalType.Brand);

                ipmApproval.ApprovalMembers.Count.Should().Be(1);
                brandApproval.ApprovalMembers.Count.Should().Be(1);
            }