Exemplo n.º 1
0
        public void GetPupil(Pupil pickedPupil, PupilDto retrievedPupil)
        {
            "When I pick a pupil from the recorded ones"
            .x(() =>
            {
                var pickedPupilIndex = new Random().Next(0, _pupils.Count);
                pickedPupil          = _pupils.ToList()[pickedPupilIndex];
            });

            "And attempt to fetch it from the its ID"
            .x(()
               => retrievedPupil = _pupilService.GetPupil(pickedPupil.Id));

            "Then it should retrieve the original record"
            .x(() =>
            {
                retrievedPupil.Id
                .Should()
                .Be(pickedPupil.Id);

                retrievedPupil.Email
                .Should()
                .Be(pickedPupil.PupilEmail);

                retrievedPupil.Nickname
                .Should()
                .Be(pickedPupil.PupilNickname);
            });
        }
Exemplo n.º 2
0
        public void TryGetCurrentPupilWithUnknownUser(PupilDto pupil, bool success)
        {
            "Given that the pupil has the required claim"
            .x(()
               => _httpContextAccessor
               .Setup(_ => _.HttpContext.User.HasClaim(It.IsAny <Predicate <Claim> >()))
               .Returns(false));

            "And that the pupil identifier does not exists in its JWT"
            .x(() =>
            {
                var mockedClaimsPrincipal = new Mock <ClaimsPrincipal>();

                mockedClaimsPrincipal
                .Setup(_ => _.FindFirst(It.IsAny <string>()))
                .Returns((Claim)null);

                _httpContextAccessor
                .SetupGet(_ => _.HttpContext.User)
                .Returns(mockedClaimsPrincipal.Object);
            });

            "When the system attempts to retrieve the current pupil"
            .x(()
               => success = _authenticationService.TryGetCurrentPupil(out pupil));

            "Then the system should not have been able to fetch the user"
            .x(() =>
            {
                pupil.Should().BeNull();
                success.Should().BeFalse();
            });
        }
Exemplo n.º 3
0
        /// <summary>
        /// This method shows the information of the selected pupil
        /// <example>Pupils/Details/1</example>
        /// <example>Pupils/Details/4</example>
        /// </summary>
        /// <param name="id">ID of the selected pupil</param>
        /// <returns>Details of the Pupil whose ID is given</returns>

        public ActionResult Details(int id)
        {
            ShowPupil showPupil = new ShowPupil();

            //Get the current pupil from the database
            string url = "Pupildata/FindPupil/" + id;
            HttpResponseMessage response = client.GetAsync(url).Result;

            if (response.IsSuccessStatusCode)
            {
                PupilDto SelectedPupil = response.Content.ReadAsAsync <PupilDto>().Result;
                showPupil.pupil = SelectedPupil;

                //Get the Classe to which the pupil belongs
                url      = "PupilData/GetPupilClasse/" + id;
                response = client.GetAsync(url).Result;
                ClasseDto SelectedClasse = response.Content.ReadAsAsync <ClasseDto>().Result;
                showPupil.classe = SelectedClasse;

                //Get the location of the selected pupil
                url      = "PupilData/GetPupilLocation/" + id;
                response = client.GetAsync(url).Result;
                LocationDto SelectedLocation = response.Content.ReadAsAsync <LocationDto>().Result;
                showPupil.location = SelectedLocation;

                return(View(showPupil));
            }
            else
            {
                return(RedirectToAction("Error"));
            }
        }
        public IHttpActionResult GetPupils()
        {
            //List of the pupils from the database
            List <Pupil> Pupils = db.Pupils.ToList();

            //Data transfer object to show information about the pupil
            List <ShowPupil> PupilDtos = new List <ShowPupil> {
            };

            foreach (var Pupil in Pupils)
            {
                ShowPupil pupil = new ShowPupil();

                //Get the Classe to which the Pupil belongs to
                Classe classe = db.Classes.Where(c => c.Pupils.Any(m => m.pId == Pupil.pId)).FirstOrDefault();

                ClasseDto parentClass = new ClasseDto
                {
                    classId   = classe.classId,
                    className = classe.className,
                    startDate = classe.startDate,
                    endDate   = classe.endDate
                };
                //Get the location of pupil
                Location location = db.Locations.Where(l => l.Pupils.Any(m => m.pId == Pupil.pId)).FirstOrDefault();

                LocationDto locationPlace = new LocationDto
                {
                    locId       = location.locId,
                    city        = location.city,
                    country     = location.country,
                    incomeRange = location.incomeRange
                };



                PupilDto NewPupil = new PupilDto
                {
                    pId       = Pupil.pId,
                    firstName = Pupil.firstName,
                    lastName  = Pupil.lastName,
                    age       = Pupil.age,
                    classId   = Pupil.classId,
                    locId     = Pupil.locId
                };

                pupil.pupil    = NewPupil;
                pupil.classe   = parentClass;
                pupil.location = locationPlace;
                PupilDtos.Add(pupil);
            }

            return(Ok(PupilDtos));
        }
Exemplo n.º 5
0
        public ActionResult Edit(int id)
        {
            EditPupil newPupil = new EditPupil();

            //Get the selected pupil from the database
            string url = "PupilData/FindPupil/" + id;
            HttpResponseMessage response = client.GetAsync(url).Result;

            if (response.IsSuccessStatusCode)
            {
                PupilDto SelectedPupil = response.Content.ReadAsAsync <PupilDto>().Result;
                newPupil.pupil = SelectedPupil;
            }
            else
            {
                return(RedirectToAction("Error"));
            }

            //Get all Classes from the database for dropdown list
            url      = "PupilData/GetClasses";
            response = client.GetAsync(url).Result;
            if (response.IsSuccessStatusCode)
            {
                IEnumerable <ClasseDto> SelectedClasses = response.Content.ReadAsAsync <IEnumerable <ClasseDto> >().Result;
                newPupil.allClasses = SelectedClasses;
            }
            else
            {
                return(RedirectToAction("Error"));
            }

            //Get all locations from the database for dropdown list
            url      = "PupilData/GetLocations";
            response = client.GetAsync(url).Result;
            if (response.IsSuccessStatusCode)
            {
                IEnumerable <LocationDto> SelectedLocations = response.Content.ReadAsAsync <IEnumerable <LocationDto> >().Result;
                newPupil.allLocations = SelectedLocations;
            }
            else
            {
                return(RedirectToAction("Error"));
            }
            return(View(newPupil));
        }
Exemplo n.º 6
0
        public void TryGetCurrentPupilWithBadRole(PupilDto pupil, bool success)
        {
            "Given that the pupil does not have the required claim"
            .x(()
               => _httpContextAccessor
               .Setup(_ => _.HttpContext.User.HasClaim(It.IsAny <Predicate <Claim> >()))
               .Returns(true));

            "When the system attempts to retrieve the current pupil"
            .x(()
               => success = _authenticationService.TryGetCurrentPupil(out pupil));

            "Then the system should not have been able to fetch the user"
            .x(() =>
            {
                pupil.Should().BeNull();
                success.Should().BeFalse();
            });
        }
        public IHttpActionResult FindPupil(int id)
        {
            Pupil pupil = db.Pupils.Find(id);

            if (pupil == null)
            {
                return(NotFound());
            }
            PupilDto pupilTemp = new PupilDto
            {
                pId       = pupil.pId,
                firstName = pupil.firstName,
                lastName  = pupil.lastName,
                age       = pupil.age,
                classId   = pupil.classId,
                locId     = pupil.locId
            };

            return(Ok(pupilTemp));
        }
Exemplo n.º 8
0
        /// <inheritdoc cref="IJwtService.GetPupilToken" />
        public string GetPupilToken(PupilDto pupil)
        {
            var claims = new List <Claim>
            {
                new Claim(ClaimTypes.Role, InTechNetRoles.Pupil),
                new Claim(ClaimTypes.NameIdentifier, pupil.Id.ToString())
            };

            var token = new JwtSecurityToken(
                _jwtResource.Issuer,
                _jwtResource.Audience,
                expires: _jwtResource.ValidUntil,
                signingCredentials: new SigningCredentials(
                    new SymmetricSecurityKey(_jwtResource.EncodedSecretKey),
                    InTechNetSecurity.JwtSigningAlgorithm
                    ),
                claims: claims);

            return(new JwtSecurityTokenHandler().WriteToken(token));
        }
Exemplo n.º 9
0
        public void TryGetCurrentPupil(PupilDto pupil, bool success)
        {
            "Given that the pupil has the required claim"
            .x(()
               => _httpContextAccessor
               .Setup(_ => _.HttpContext.User.HasClaim(It.IsAny <Predicate <Claim> >()))
               .Returns(false));

            "And that the pupil has the NameIdentifier claim"
            .x(() =>
            {
                var mockedClaimsPrincipal = new Mock <ClaimsPrincipal>();

                mockedClaimsPrincipal
                .Setup(_ => _.FindFirst(It.IsAny <string>()))
                .Returns(
                    new Claim(ClaimTypes.NameIdentifier, _fixture.Create <int>().ToString()));

                _httpContextAccessor
                .SetupGet(_ => _.HttpContext.User)
                .Returns(mockedClaimsPrincipal.Object);
            });


            "When the system retrieves the current moderator"
            .x(() =>
            {
                _pupilService
                .Setup(_ => _.GetPupil(It.IsAny <int>()))
                .Returns(new PupilDto());

                success = _authenticationService.TryGetCurrentPupil(out pupil);
            });

            "Then the system should have successfully fetch the moderator"
            .x(() =>
            {
                pupil.Should().NotBeNull();
                success.Should().BeTrue();
            });
        }
        public IHttpActionResult GetLocationPupils(int id)
        {
            //List of pupil which location ID is the same as the selected location ID
            List <Pupil>    pupils    = db.Pupils.Where(p => p.locId == id).ToList();
            List <PupilDto> PupilDtos = new List <PupilDto> {
            };

            foreach (var pupil in pupils)
            {
                PupilDto NewPupil = new PupilDto
                {
                    pId       = pupil.pId,
                    firstName = pupil.firstName,
                    lastName  = pupil.lastName,
                    age       = pupil.age,
                };
                PupilDtos.Add(NewPupil);
            }

            return(Ok(PupilDtos));
        }
Exemplo n.º 11
0
        public void AuthenticatePupilByNickname(string originalPassword, Pupil registeredPupil, AuthenticationDto pupil,
                                                PupilDto authenticatedPupil)
        {
            "Given a password"
            .x(()
               => originalPassword = _fixture.Create <string>());

            "And a pupil already registered using this password"
            .x(() =>
            {
                registeredPupil = _fixture.Create <Pupil>();

                var hashedPassword            = originalPassword.HashedWith(registeredPupil.PupilSalt);
                registeredPupil.PupilPassword = hashedPassword;

                _pupils.Add(registeredPupil);
            });

            "When a pupil connect to its account with its original password and its nickname"
            .x(()
               =>
            {
                pupil = _fixture.Build <AuthenticationDto>()
                        .With(_ => _.Login, registeredPupil.PupilNickname)
                        .With(_ => _.Password, originalPassword)
                        .Create();

                authenticatedPupil = _pupilService.AuthenticatePupil(pupil);
            });

            "Then we should retrieve the original Pupil"
            .x(() =>
            {
                authenticatedPupil.Email.Should()
                .Be(registeredPupil.PupilEmail);

                authenticatedPupil.Nickname.Should()
                .Be(registeredPupil.PupilNickname);
            });
        }
        public IActionResult OnGet(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var pupil = _unitOfWork.PupilRepository.GetById(id.Value);

            if (pupil == null)
            {
                return(NotFound());
            }
            Pupil = new PupilDto
            {
                FirstName = pupil.FirstName,
                LastName  = pupil.LastName,
                Id        = pupil.Id,
                Version   = pupil.Version
            };
            return(Page());
        }
        public IHttpActionResult GetClassePupils(int id)
        {
            //List of all pupils registered in the current Classe (course)
            List <Pupil>    Pupils    = db.Pupils.Where(p => p.classId == id).ToList();
            List <PupilDto> PupilDtos = new List <PupilDto> {
            };

            foreach (var Pupil in Pupils)
            {
                PupilDto NewPupil = new PupilDto
                {
                    pId       = Pupil.pId,
                    firstName = Pupil.firstName,
                    lastName  = Pupil.lastName,
                    age       = Pupil.age,
                    classId   = Pupil.classId,
                    locId     = Pupil.locId
                };
                PupilDtos.Add(NewPupil);
            }

            return(Ok(PupilDtos));
        }
Exemplo n.º 14
0
        public void GetAuthenticatedPupil(PupilDto pupil)
        {
            "Given that the authentication is successful"
            .x(()
               => _pupilService
               .Setup(_ => _.AuthenticatePupil(It.IsAny <AuthenticationDto>()))
               .Returns(new PupilDto()));

            "When the pupil authenticate itself"
            .x(() =>
            {
                _jwtService
                .Setup(_ => _.GetPupilToken(It.IsAny <PupilDto>()))
                .Returns(_fixture.Create <string>());

                pupil = _authenticationService.GetAuthenticatedPupil(
                    _fixture.Create <AuthenticationDto>());
            });

            "Then the pupil should have a token"
            .x(()
               => pupil.Token.Should()
               .NotBeNullOrEmpty());
        }