public async Task GetDmarcReadModelDomainHasDmarcRecordDmarcRecordReturned() { int domainId = 1; string readmodel = "{\"readModel\": \"Test\"}"; string domainName = "abc.xyz.com"; DomainRequest request = new DomainRequest { Id = domainId }; ValidationResult validationResult = new ValidationResult(new List <ValidationFailure>()); A.CallTo(() => _domainRequestValidator.ValidateAsync(request, CancellationToken.None)).Returns(Task.FromResult(validationResult)); DmarcReadModel dmarcReadModel = new DmarcReadModel(new Domain.Domain(domainId, domainName), true, readmodel); A.CallTo(() => _domainStatusDao.GetDmarcReadModel(request.Id)).Returns(Task.FromResult(dmarcReadModel)); IActionResult result = await _domainStatusController.GetDmarcReadModel(request); ObjectResult objectResult = result as ObjectResult; Assert.That(objectResult.Value, Is.TypeOf <string>()); string dmarcReadModelString = objectResult.Value as string; Assert.That(dmarcReadModelString, Is.EqualTo(readmodel)); A.CallTo(() => _domainStatusDao.GetDmarcReadModel(A <int> ._)).MustHaveHappened(Repeated.Exactly.Once); A.CallTo(() => _organisationalDomainProvider.GetOrganisationalDomain(A <string> ._)).MustNotHaveHappened(); A.CallTo(() => _domainStatusDao.GetDmarcReadModel(A <string> ._)).MustNotHaveHappened(); }
public async Task <IActionResult> GetDmarcReadModel(DomainRequest domainRequest) { ValidationResult validationResult = await _domainRequestValidator.ValidateAsync(domainRequest); if (!validationResult.IsValid) { _log.LogWarning($"Bad request: {validationResult.GetErrorString()}"); return(BadRequest(new ErrorResponse(validationResult.GetErrorString()))); } DmarcReadModel dmarc = await _domainStatusDao.GetDmarcReadModel(domainRequest.Id); if (dmarc == null) { Domain.Domain domain = await _domainStatusDao.GetDomain(domainRequest.Id); if (domain == null) { return(NotFound(new ErrorResponse($"No domain found for ID {domainRequest.Id}."))); } return(new ObjectResult(new { records = (List <string>)null, pending = true })); } if (dmarc.HasDmarc) { return(new ObjectResult(dmarc.Model)); } OrganisationalDomain organisationalDomain = await _organisationalDomainProvider.GetOrganisationalDomain(dmarc.Domain.Name); if (organisationalDomain.IsOrgDomain || organisationalDomain.IsTld) { return(new ObjectResult(dmarc.Model)); } DmarcReadModel organisationalDomainDmarcRecord = await _domainStatusDao.GetDmarcReadModel(organisationalDomain.OrgDomain); if (organisationalDomainDmarcRecord == null) { return(new ObjectResult(dmarc.Model)); } JObject readModel = JObject.Parse(organisationalDomainDmarcRecord.Model); readModel.AddFirst(new JProperty("inheritedFrom", JToken.FromObject(organisationalDomainDmarcRecord.Domain, new JsonSerializer { ContractResolver = new CamelCasePropertyNamesContractResolver() }))); return(new ObjectResult(readModel.ToString())); }
public async Task GetDmarcReadModelForDomainWhenDomainDoesntHaveDmarcAndOrgDomainDoesReturnsOrgDomainResult() { int domainId = 1; string readmodel = "{\"readModel\": \"Test\"}"; string orgDomainReadModel = "{\"readModel\": \"Test\"}"; string domainName = "abc.xyz.com"; string organisationalDomainName = "xyz.com"; DomainRequest request = new DomainRequest { Id = domainId }; ValidationResult validationResult = new ValidationResult(new List <ValidationFailure>()); A.CallTo(() => _domainRequestValidator.ValidateAsync(request, CancellationToken.None)).Returns(Task.FromResult(validationResult)); DmarcReadModel dmarcReadModel = new DmarcReadModel(new Domain.Domain(1, domainName), false, readmodel); A.CallTo(() => _domainStatusDao.GetDmarcReadModel(request.Id)).Returns(Task.FromResult(dmarcReadModel)); OrganisationalDomain organisationalDomain = new OrganisationalDomain(organisationalDomainName, domainName); A.CallTo(() => _organisationalDomainProvider.GetOrganisationalDomain(domainName)).Returns(Task.FromResult(organisationalDomain)); DmarcReadModel orgDomainDmarcReadModel = new DmarcReadModel(new Domain.Domain(1, domainName), false, orgDomainReadModel); A.CallTo(() => _domainStatusDao.GetDmarcReadModel(A <string> ._)).Returns(Task.FromResult(orgDomainDmarcReadModel)); IActionResult result = await _domainStatusController.GetDmarcReadModel(request); ObjectResult objectResult = result as ObjectResult; Assert.That(objectResult.Value, Is.TypeOf <string>()); string dmarcReadModelString = objectResult.Value as string; Assert.That(dmarcReadModelString, Does.Contain("\"readModel\": \"Test\"")); Assert.That(dmarcReadModelString, Does.Contain("inheritedFrom")); A.CallTo(() => _domainStatusDao.GetDmarcReadModel(A <int> ._)).MustHaveHappened(Repeated.Exactly.Once); A.CallTo(() => _organisationalDomainProvider.GetOrganisationalDomain(A <string> ._)).MustHaveHappened(Repeated.Exactly.Once); A.CallTo(() => _domainStatusDao.GetDmarcReadModel(A <string> ._)).MustHaveHappened(Repeated.Exactly.Once); }