public async Task InsertKwet_InvalidJWT_ThrowsException()
        {
            var          userGuid  = Guid.NewGuid();
            var          timeStamp = DateTime.Now;
            const string jwt       = "";

            var newkwet = new NewKwetModel()
            {
                Id       = userGuid.ToString(),
                Message  = "This is my placed Kwet",
                UserName = "******"
            };

            var returnKwet = new Kwet()
            {
                KwetId    = new Guid(),
                UserId    = userGuid,
                UserName  = "******",
                Message   = "This is my placed Kwet",
                TimeStamp = timeStamp,
                Likes     = new List <Likes>()
            };

            _jwtIdClaimReaderHelper.Setup(x => x.getUserIdFromToken(jwt))
            .Returns(userGuid);
            _kwetService.Setup(x => x.InsertKwet(It.IsAny <NewKwetModel>(), jwt))
            .Throws <JwtInvalidException>();
            var result = await _kwetController.Insert(newkwet, jwt) as ObjectResult;

            Assert.IsType <BadRequestObjectResult>(result);
        }
        public async Task InsertKwet_ValidKwet_ReturnsKwet()
        {
            var          userGuid  = Guid.NewGuid();
            var          timeStamp = DateTime.Now;
            const string jwt       = "";

            var newkwet = new NewKwetModel()
            {
                Id       = userGuid.ToString(),
                Message  = "This is my placed Kwet",
                UserName = "******"
            };

            var returnKwet = new Kwet()
            {
                KwetId    = new Guid(),
                UserId    = userGuid,
                UserName  = "******",
                Message   = "This is my placed Kwet",
                TimeStamp = timeStamp,
                Likes     = new List <Likes>()
            };

            _jwtIdClaimReaderHelper.Setup(x => x.getUserIdFromToken(jwt)).Returns(userGuid);
            _kwetService.Setup(x => x.InsertKwet(It.IsAny <NewKwetModel>(), jwt)).ReturnsAsync(returnKwet);
            var result = await _kwetController.Insert(newkwet, jwt) as ObjectResult;

            Assert.IsType <OkObjectResult>(result);
            Assert.Equal(returnKwet.KwetId, ((Kwet)result.Value).KwetId);
        }
Example #3
0
        public async Task InsertKwets_InvalidJWT_ThrowsException()
        {
            var          userGuid  = Guid.NewGuid();
            var          timeStamp = DateTime.Now;
            const string jwt       = "";

            var newkwet = new NewKwetModel()
            {
                Id       = userGuid.ToString(),
                Message  = "This is my placed Kwet",
                UserName = "******"
            };

            var returnKwet = new Kwet()
            {
                KwetId    = new Guid(),
                UserId    = userGuid,
                UserName  = "******",
                Message   = "This is my placed Kwet",
                TimeStamp = timeStamp,
                Likes     = new List <Likes>()
            };

            _repository.Setup(x => x.Create(It.IsAny <Kwet>())).ReturnsAsync(returnKwet);
            _jwtIdClaimReaderHelper.Setup(x => x.getUserIdFromToken(jwt)).Returns(Guid.Empty);

            var result = await Assert.ThrowsAsync <JwtInvalidException>(() =>
                                                                        _kwetService.InsertKwet(newkwet, jwt));

            Assert.IsType <JwtInvalidException>(result);
        }
Example #4
0
        public async Task InsertKwets_ValidKwet_ReturnsPlacedKwet()
        {
            var          userGuid  = Guid.NewGuid();
            var          timeStamp = DateTime.Now;
            const string jwt       = "";

            var newkwet = new NewKwetModel()
            {
                Id       = userGuid.ToString(),
                Message  = "This is my placed Kwet",
                UserName = "******"
            };

            var returnKwet = new Kwet()
            {
                KwetId    = new Guid(),
                UserId    = userGuid,
                UserName  = "******",
                Message   = "This is my placed Kwet",
                TimeStamp = timeStamp,
                Likes     = new List <Likes>()
            };

            _repository.Setup(x => x.Create(It.IsAny <Kwet>())).ReturnsAsync(returnKwet);
            _jwtIdClaimReaderHelper.Setup(x => x.getUserIdFromToken(jwt)).Returns(userGuid);

            var result = await _kwetService.InsertKwet(newkwet, jwt);

            Assert.Equal(returnKwet.Message, result.Message);
            Assert.Equal(returnKwet.KwetId, result.KwetId);
        }
Example #5
0
 public async Task <IActionResult> Insert([FromBody] NewKwetModel kwetModel, [FromHeader(Name = "Authorization")] string token)
 {
     Console.Out.WriteLine("message recieved");
     Console.Out.WriteLine(kwetModel);
     try
     {
         return(Ok(await _service.InsertKwet(kwetModel, token)));
     }
     catch (Exception e)
     {
         return(BadRequest(e.Message));
     }
 }
Example #6
0
        public async Task <Kwet> InsertKwet(NewKwetModel kwet, string token)
        {
            if (Guid.Parse(kwet.Id) != _jwtIdClaimReaderHelper.getUserIdFromToken(token))
            {
                throw new JwtInvalidException();
            }
            var newKwet = new Kwet
            {
                KwetId    = new Guid(),
                UserId    = Guid.Parse(kwet.Id),
                UserName  = kwet.UserName,
                Message   = kwet.Message,
                TimeStamp = DateTime.Now,
                Likes     = new List <Likes>()
            };

            return(await _repository.Create(newKwet));
        }