Example #1
0
        public async Task GetStudentsInGroup_GroupWithStudentsAndTeachers_ReturnsOnlyStudents()
        {
            //arrange
            var group = new Group("g1", userId);
            var organizationMembers = new List <AppUser>
            {
                AppUser.CreateOrganizationMember("*****@*****.**", "student1", false, userId),
                AppUser.CreateOrganizationMember("*****@*****.**", "student2", false, userId),
                AppUser.CreateOrganizationMember("*****@*****.**", "student3", false, userId),
                AppUser.CreateOrganizationMember("*****@*****.**", "teacher1", true, userId),
            };

            dbContext.Users.AddRange(organizationMembers);
            await dbContext.SaveChangesAsync();

            foreach (var organizationMember in organizationMembers)
            {
                group.AddMember(organizationMember.Id);
            }

            InsertData(group);
            var groupService = new GroupService(dbContext, userContext);

            //act
            var result = await groupService.GetStudentsInGroup(group.Id);

            //assert
            result.Succeeded.Should().BeTrue();
            result.Data.Should().HaveCount(3);
            var expectedStudentsIds = organizationMembers.Where(x => !x.IsTeacher).Select(x => x.Id);

            result.Data.Select(x => x.Id).Should().BeEquivalentTo(expectedStudentsIds);
        }
Example #2
0
        private async Task AddOrganizationMembers()
        {
            foreach (var owner in OrganizationOwners)
            {
                if (owner.Id == 0)
                {
                    throw new Exception("Wrong user ID");
                }

                for (int i = 1; i <= 3; i++)
                {
                    var username = $"user{i}_invitedby_{owner.Id}";
                    var email    = username + "@example.com";
                    var member   = AppUser.CreateOrganizationMember(email, username, false, owner.Id);
                    if (OrganizationOwnerMembers.ContainsKey(owner.Id))
                    {
                        OrganizationOwnerMembers[owner.Id].Add(member);
                    }
                    else
                    {
                        OrganizationOwnerMembers.Add(owner.Id, new List <AppUser> {
                            member
                        });
                    }
                }
            }

            foreach (var member in OrganizationOwnerMembers.Values.SelectMany(x => x))
            {
                await AddUser(member);
            }
        }
Example #3
0
        public async Task <OperationResult <Unit> > RegisterUser(string code, string email, string username, string password)
        {
            var invitation = await dbContext.Invitations.FirstOrDefaultAsync(x => x.Code == code);

            if (invitation == null || invitation.Email.ToUpperInvariant() != email.ToUpperInvariant())
            {
                return(new BadRequestError());
            }

            var user   = AppUser.CreateOrganizationMember(email, username, invitation.IsTeacher, invitation.InvitedBy);
            var result = await CreateUser(password, user);

            if (result.Succeeded)
            {
                dbContext.Invitations.Remove(invitation);
                await dbContext.SaveChangesAsync();
            }
            return(result);
        }
Example #4
0
        public void MapToUserDto_AppUser_ValidDto(bool isTeacher)
        {
            //arrange
            var userId      = 38;
            var username    = "******";
            var email       = "*****@*****.**";
            var user        = AppUser.CreateOrganizationMember(userId, email, username, isTeacher, 3);
            var expectedDto = new UserDto
            {
                Email     = email,
                Id        = userId,
                IsTeacher = isTeacher,
                Username  = username
            };

            //act
            var mapper = new OrganizationServiceMapper();
            var dto    = mapper.MapToUserDto(user);

            //assert
            dto.Should().BeEquivalentTo(expectedDto);
        }
Example #5
0
        public async Task RemoveMemberFromGroup_IsMember_ReturnsSuccessfulResult()
        {
            //arrange
            int groupOwner  = userId;
            var group       = new Group("group", groupOwner);
            var groupMember = AppUser.CreateOrganizationMember("*****@*****.**", "user1", false, groupOwner);

            InsertData(groupMember);
            int groupMemberId = groupMember.Id;

            group.AddMember(groupMemberId);
            InsertData(group);
            var groupService = new GroupService(dbContext, userContext);

            //act
            var result = await groupService.RemoveUserFromGroup(groupMemberId, group.Id);

            //assert
            result.Succeeded.Should().BeTrue();
            dbContext.GroupMembers
            .FirstOrDefault(x => x.GroupId == group.Id && x.UserId == groupMemberId)
            .Should().BeNull();
        }