Beispiel #1
0
        public async Task FetchInvitationsAsync_WithClanId_ShouldBeOfTypeInvitationList()
        {
            // Arrange
            var clanId = new ClanId();

            TestMock.InvitationRepository.Setup(repository => repository.FetchAsync(It.IsAny <ClanId>()))
            .ReturnsAsync(
                new List <Invitation>
            {
                new Invitation(new UserId(), clanId),
                new Invitation(new UserId(), clanId),
                new Invitation(new UserId(), clanId)
            })
            .Verifiable();

            var service = new InvitationService(TestMock.InvitationRepository.Object, TestMock.ClanRepository.Object);

            // Act
            var result = await service.FetchInvitationsAsync(new ClanId());

            // Assert
            result.Should().BeOfType <List <Invitation> >();

            TestMock.InvitationRepository.Verify(repository => repository.FetchAsync(It.IsAny <ClanId>()), Times.Once);
        }
        public async Task FetchDivisionsAsync_ShouldBeOfTypeOkObjectResult()
        {
            // Arrange
            var clanId = new ClanId();

            TestMock.ClanService.Setup(clanService => clanService.FetchDivisionsAsync(It.IsAny <ClanId>()))
            .ReturnsAsync(
                new List <Division>
            {
                new Division(clanId, "Test", "Division"),
                new Division(clanId, "Test", "Division"),
                new Division(clanId, "Test", "Division")
            })
            .Verifiable();

            var clanDivisionsController = new ClanDivisionsController(TestMock.ClanService.Object, TestMapper)
            {
                ControllerContext =
                {
                    HttpContext = MockHttpContextAccessor.GetInstance()
                }
            };

            // Act
            var result = await clanDivisionsController.FetchDivisionsAsync(clanId);

            // Assert
            result.Should().BeOfType <OkObjectResult>();

            TestMock.ClanService.Verify(clanService => clanService.FetchDivisionsAsync(It.IsAny <ClanId>()), Times.Once);
        }
Beispiel #3
0
 public Division(ClanId clanId, string name, string description) : this()
 {
     ClanId      = clanId;
     Name        = name;
     Description = description;
     Members     = new HashSet <Member>();
 }
Beispiel #4
0
        public async Task DeleteInvitationsAsync_WithClanId()
        {
            // Arrange
            var clanId = new ClanId();

            TestMock.InvitationRepository.Setup(repository => repository.FetchAsync(It.IsAny <ClanId>()))
            .ReturnsAsync(
                new List <Invitation>
            {
                new Invitation(new UserId(), clanId),
                new Invitation(new UserId(), clanId),
                new Invitation(new UserId(), clanId)
            })
            .Verifiable();

            TestMock.InvitationRepository.Setup(repository => repository.Delete(It.IsAny <Invitation>())).Verifiable();

            TestMock.InvitationRepository.Setup(repository => repository.UnitOfWork.CommitAsync(It.IsAny <bool>(), It.IsAny <CancellationToken>()))
            .Returns(Task.CompletedTask)
            .Verifiable();

            var service = new InvitationService(TestMock.InvitationRepository.Object, TestMock.ClanRepository.Object);

            // Act
            await service.DeleteInvitationsAsync(new ClanId());

            // Assert
            TestMock.InvitationRepository.Verify(repository => repository.FetchAsync(It.IsAny <ClanId>()), Times.Once);
            TestMock.InvitationRepository.Verify(repository => repository.Delete(It.IsAny <Invitation>()), Times.Exactly(3));
            TestMock.InvitationRepository.Verify(repository => repository.UnitOfWork.CommitAsync(It.IsAny <bool>(), It.IsAny <CancellationToken>()), Times.Once);
        }
Beispiel #5
0
        public void Contructor_Tests()
        {
            // Arrange
            var ownerId = new UserId();
            var clanId  = new ClanId();

            var name        = "test";
            var description = "division";

            // Act
            var division = new Division(clanId, name, description);

            // Assert
            division.Id.Should().BeOfType(typeof(DivisionId));

            division.Name.Should().Be(name);
            division.Name.Should().NotBeNull();

            division.Description.Should().Be(description);
            division.Description.Should().NotBeNull();

            division.Members.Should().BeOfType(typeof(HashSet <Member>));
            division.Members.Should().HaveCount(0);
            division.Members.Should().NotBeNull();
        }
Beispiel #6
0
        public async Task <IActionResult> UploadLogoAsync(ClanId clanId, [FromForm] IFormFile logo)
        {
            var userId = HttpContext.GetUserId();

            var clan = await _clanService.FindClanAsync(clanId);

            if (clan == null)
            {
                return(this.NotFound("Clan does not exist."));
            }

            var result = await _clanService.UploadLogoAsync(
                clan,
                userId,
                logo.OpenReadStream(),
                logo.FileName);

            if (result.IsValid)
            {
                return(this.Ok("The logo has been uploaded."));
            }

            result.AddToModelState(ModelState);

            return(this.BadRequest(new ValidationProblemDetails(ModelState)));
        }
Beispiel #7
0
        public async Task <DomainValidationResult <Invitation> > SendInvitationAsync(ClanId clanId, UserId userId, UserId ownerId)
        {
            var result = new DomainValidationResult <Invitation>();

            if (!await _clanRepository.IsOwnerAsync(clanId, ownerId))
            {
                result.AddDebugError("Permission required.");
            }

            if (await _clanRepository.IsMemberAsync(userId))
            {
                result.AddDebugError("Target already in a clan.");
            }

            if (await _invitationRepository.ExistsAsync(ownerId, clanId))
            {
                result.AddFailedPreconditionError("The invitation from this clan to that member already exist.");
            }

            if (result.IsValid)
            {
                var invitation = new Invitation(userId, clanId);

                _invitationRepository.Create(invitation);

                await _invitationRepository.UnitOfWork.CommitAsync();

                return(invitation);
            }

            return(result);
        }
        public async Task <IActionResult> RemoveMemberFromDivisionAsync(ClanId clanId, DivisionId divisionId, MemberId memberId)
        {
            var userId = HttpContext.GetUserId();

            var clan = await _clanService.FindClanAsync(clanId);

            if (clan == null)
            {
                return(this.NotFound("Clan does not exist."));
            }

            var result = await _clanService.RemoveMemberFromDivisionAsync(
                clan,
                userId,
                divisionId,
                memberId);

            if (result.IsValid)
            {
                return(this.Ok("The division has been removed."));
            }

            result.AddToModelState(ModelState);

            return(this.BadRequest(new ValidationProblemDetails(ModelState)));
        }
Beispiel #9
0
        public async Task <IActionResult> UpdateDivisionAsync(ClanId clanId, DivisionId divisionId, UpdateDivisionRequest request)
        {
            var userId = HttpContext.GetUserId();

            var clan = await _clanService.FindClanAsync(clanId);

            if (clan == null)
            {
                return(this.NotFound("Clan does not exist."));
            }

            var result = await _clanService.UpdateDivisionAsync(
                clan,
                userId,
                divisionId,
                request.Name,
                request.Description);

            if (result.IsValid)
            {
                return(this.Ok("Division Updated."));
            }

            result.AddToModelState(ModelState);

            return(this.BadRequest(new ValidationProblemDetails(ModelState)));
        }
Beispiel #10
0
        public async Task AddMemberToClanAsync(ClanId clanId, IMemberInfo memberInfo)
        {
            var clan = await _clanRepository.FindClanAsync(clanId) ?? throw new InvalidOperationException(nameof(this.AddMemberToClanAsync));

            clan.AddMember(memberInfo);

            await _clanRepository.UnitOfWork.CommitAsync();
        }
        public void Validate_WhenClanIdIsValid_ShouldNotHaveValidationErrorFor(ClanId clanId)
        {
            // Arrange
            var validator = new InvitationPostRequestValidator();

            // Act - Assert
            validator.ShouldNotHaveValidationErrorFor(request => request.ClanId, clanId.ToString());
        }
Beispiel #12
0
        public async Task DeleteCandidaturesAsync(ClanId clanId)
        {
            foreach (var candidature in await this.FetchCandidaturesAsync(clanId))
            {
                _candidatureRepository.Delete(candidature);
            }

            await _candidatureRepository.UnitOfWork.CommitAsync();
        }
Beispiel #13
0
 private async Task <HttpResponseMessage> ExecuteAsync(ClanId clanId, FileStream file)
 {
     return(await _httpClient.PostAsync(
                $"api/clans/{clanId}/logo",
                new MultipartFormDataContent
     {
         { new StreamContent(file), "logo", "edoxa.png" }
     }));
 }
Beispiel #14
0
        public async Task DeleteInvitationsAsync(ClanId clanId)
        {
            foreach (var invitation in await this.FetchInvitationsAsync(clanId))
            {
                _invitationRepository.Delete(invitation);
            }

            await _invitationRepository.UnitOfWork.CommitAsync();
        }
Beispiel #15
0
        public async Task UploadLogoAsync(ClanId clanId, Stream stream, string fileName)
        {
            var container = _storageAccount.GetBlobContainer();

            var directory = container.GetDirectoryReference($"organizations/clans/{clanId}/logo");

            var blockBlob = directory.GetBlockBlobReference($"{new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds()}{Path.GetExtension(fileName)}");

            await blockBlob.UploadFromStreamAsync(stream);
        }
Beispiel #16
0
        public async Task DeleteLogoAsync(ClanId clanId)
        {
            var container = _storageAccount.GetBlobContainer();

            var directory = container.GetDirectoryReference($"organizations/clans/{clanId}/logo");

            foreach (var blockBlob in directory.ListBlobs().Cast <CloudBlockBlob>().ToList())
            {
                await blockBlob.DeleteIfExistsAsync();
            }
        }
Beispiel #17
0
 private async Task DeleteInvitationsAsync(ClanId clanId)
 {
     try
     {
         await _candidatureService.DeleteCandidaturesAsync(clanId);
     }
     catch (Exception exception)
     {
         _logger.LogCritical(exception, "The clan's user candidatures have not been deleted correctly.");
     }
 }
Beispiel #18
0
        public async Task <IActionResult> FetchDivisionsAsync(ClanId clanId)
        {
            var divisions = await _clanService.FetchDivisionsAsync(clanId);

            if (!divisions.Any())
            {
                return(this.NoContent());
            }

            return(this.Ok(_mapper.Map <IEnumerable <DivisionDto> >(divisions)));
        }
Beispiel #19
0
        public async Task <IActionResult> FindClanAsync(ClanId clanId)
        {
            var clan = await _clanService.FindClanAsync(clanId);

            if (clan == null)
            {
                return(this.NotFound("Clan not found."));
            }

            return(this.Ok(_mapper.Map <ClanDto>(clan)));
        }
Beispiel #20
0
        /// <summary>
        /// Returns the <see cref="Clan"/> that matches the id.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentException"><paramref name="id"/> must be a valid <see cref="ClanId"/>.</exception>
        public static Clan GetClanById(ClanId id)
        {
            var result = ClanList.FirstOrDefault <Clan>(c => c.ClanId == id);

            if (result == null)
            {
                throw new ArgumentException(nameof(id), "Must be a valid " + nameof(ClanId));
            }

            return(result);
        }
Beispiel #21
0
        public void HasMember_WithMemberId_ShouldBeTrue()
        {
            // Arrange
            var clanId = new ClanId();

            var division = new Division(clanId, "test", "division");

            division.AddMember(new Member(clanId, new UserId()));

            var memberId = division.Members.SingleOrDefault()?.Id;

            // Act Assert
            division.HasMember(memberId).Should().BeTrue();
        }
Beispiel #22
0
        public async Task HandleAsync_WhenClanMemberRemovedIntegrationEventIsValid_ShouldBeCompletedTask()
        {
            // Arrange
            var userId = new UserId();
            var clanId = new ClanId();
            var user   = new User();

            var mockLogger = new MockLogger <ClanMemberRemovedIntegrationEventHandler>();

            TestMock.UserService.Setup(userService => userService.FindByIdAsync(It.IsAny <string>())).ReturnsAsync(user).Verifiable();

            TestMock.UserService.Setup(userService => userService.RemoveClaimAsync(It.IsAny <User>(), It.IsAny <Claim>()))
            .ReturnsAsync(new IdentityResult())
            .Verifiable();

            var handler = new ClanMemberRemovedIntegrationEventHandler(TestMock.UserService.Object, mockLogger.Object);

            var integrationEvent = new ClanMemberRemovedIntegrationEvent
            {
                Clan = new ClanDto
                {
                    Id      = clanId,
                    Name    = "testClan",
                    OwnerId = userId,
                    Summary = null,
                    Members =
                    {
                        new MemberDto
                        {
                            ClanId = clanId,
                            Id     = new MemberId(),
                            UserId = userId
                        }
                    }
                },
                UserId = userId
            };

            // Act
            await handler.HandleAsync(integrationEvent);

            // Assert
            TestMock.UserService.Verify(userService => userService.FindByIdAsync(It.IsAny <string>()), Times.Once);
            TestMock.UserService.Verify(userService => userService.RemoveClaimAsync(It.IsAny <User>(), It.IsAny <Claim>()), Times.Once);
            mockLogger.Verify(Times.Once());
        }
Beispiel #23
0
        public void Update_WithParameters_ShouldChange(string name, string description)
        {
            // Arrange
            var clanId = new ClanId();

            var division = new Division(clanId, "test", "division");

            // Act
            division.Update(name, description);

            // Assert
            division.Name.Should().Be(name);
            division.Name.Should().NotBeNull();

            division.Description.Should().Be(description);
            division.Description.Should().NotBeNull();
        }
Beispiel #24
0
        public void AddMember_WithAmount_ShouldHaveCount(int amount)
        {
            // Arrange
            var clanId = new ClanId();

            var division = new Division(clanId, "test", "division");

            // Act
            for (var i = 0; i < amount; i++)
            {
                division.AddMember(new Member(clanId, new UserId()));
            }

            // Assert
            division.Members.Should().NotBeNull();
            division.Members.Should().HaveCount(amount);
        }
Beispiel #25
0
        public void Contructor_Tests()
        {
            // Arrange
            var userId = new UserId();
            var clanId = new ClanId();

            // Act
            var candidature = new Candidature(userId, clanId);

            // Assert
            candidature.Id.Should().BeOfType(typeof(CandidatureId));

            candidature.UserId.Should().Be(userId);
            candidature.UserId.Should().NotBeNull();

            candidature.ClanId.Should().Be(clanId);
            candidature.ClanId.Should().NotBeNull();
        }
Beispiel #26
0
        public async Task <IActionResult> FetchMembersAsync(ClanId clanId)
        {
            var clan = await _clanService.FindClanAsync(clanId);

            if (clan == null)
            {
                return(this.NotFound("Clan not found."));
            }

            var members = await _clanService.FetchMembersAsync(clan);

            if (!members.Any())
            {
                return(this.NoContent());
            }

            return(this.Ok(_mapper.Map <IEnumerable <MemberDto> >(members)));
        }
Beispiel #27
0
        public void Contructor_Tests()
        {
            // Arrange
            var userId = new UserId();
            var clanId = new ClanId();

            // Act
            var invitation = new Invitation(userId, clanId);

            // Assert
            invitation.Id.Should().BeOfType(typeof(InvitationId));

            invitation.UserId.Should().Be(userId);
            invitation.UserId.Should().NotBeNull();

            invitation.ClanId.Should().Be(clanId);
            invitation.ClanId.Should().NotBeNull();
        }
Beispiel #28
0
        public void Contructor_WithInvitation_Tests()
        {
            // Arrange
            var userId = new UserId();
            var clanId = new ClanId();

            // Act
            var member = new Member(new Invitation(userId, clanId));

            // Assert
            member.Id.Should().BeOfType(typeof(MemberId));

            member.UserId.Should().Be(userId);
            member.UserId.Should().NotBeNull();

            member.ClanId.Should().Be(clanId);
            member.ClanId.Should().NotBeNull();
        }
Beispiel #29
0
        public async Task <IActionResult> DownloadLogoAsync(ClanId clanId)
        {
            var clan = await _clanService.FindClanAsync(clanId);

            if (clan == null)
            {
                return(this.NotFound("Clan doesn't exist."));
            }

            var logo = await _clanService.DownloadLogoAsync(clan);

            if (logo.Length == 0)
            {
                return(this.NoContent());
            }

            return(this.File(logo, "application/octet-stream"));
        }
Beispiel #30
0
        public override global::System.Int32 GetHashCode()
        {
            unchecked
            {
                int hash = 5;
                hash ^= 397 * ClanId.GetHashCode();
                hash ^= 397 * CreatedAt.GetHashCode();
                hash ^= 397 * MembersCount.GetHashCode();
                if (Name != null)
                {
                    hash ^= 397 * Name.GetHashCode();
                }

                if (Tag != null)
                {
                    hash ^= 397 * Tag.GetHashCode();
                }

                return(hash);
            }
        }