public IActionResult Post([FromBody] MedicalRepresentative representative) { if (representative == null) { logger.Error("NULL object received in " + nameof(AuthController)); return(StatusCode(500)); } if (string.IsNullOrEmpty(representative.Email) || string.IsNullOrEmpty(representative.Password)) { logger.Info("Email or password is null"); return(BadRequest("Email/Password cannot be null")); } try { if (provider.Validate(representative)) { TokenGenerator generator = new TokenGenerator(Configuration); string token = generator.GenerateToken(); logger.Info("Token received"); return(Ok(token)); } logger.Error("Unauthorized access"); return(Unauthorized("Invalid Credentials")); } catch (Exception e) { logger.Error("Internal server error in " + nameof(AuthController) + "\n" + e.Message); return(StatusCode(500)); } }
public void ValidateTestFail() { var mock = new Mock <IMedicalRepresentative>(); mock.Setup(x => x.GetMedicalRepresentatives()).Returns(medicicalrep); MedicalRepresentativeProvider prov = new MedicalRepresentativeProvider(mock.Object); MedicalRepresentative obj = new MedicalRepresentative { Name = "Prithwiman", Email = "*****@*****.**", Password = "******" }; bool res = prov.Validate(obj); Assert.AreEqual(false, res); }
public void AuthControllerNullTest() { var mock = new Mock <IMedicalRepresentative>(); mock.Setup(x => x.GetMedicalRepresentatives()).Returns(medicicalrep); var config = new Mock <IConfiguration>(); config.Setup(c => c["TokenInfo:SecretKey"]).Returns("SomeSecretasdasfdasdajsdajdajdasda"); config.Setup(c => c["TokenInfo:Issuer"]).Returns("someissuer.com"); MedicalRepresentativeProvider provider = new MedicalRepresentativeProvider(mock.Object); AuthController controller = new AuthController(config.Object, provider); MedicalRepresentative obj = new MedicalRepresentative(); var data = controller.Post(obj); var result = data as ObjectResult; Assert.AreEqual(400, result.StatusCode); }
public void AuthControllerUnauthorizedTest() { var mock = new Mock <IMedicalRepresentative>(); mock.Setup(x => x.GetMedicalRepresentatives()).Returns(medicicalrep); var config = new Mock <IConfiguration>(); config.Setup(c => c["TokenInfo:SecretKey"]).Returns("SomeSecretasdasfdasdajsdajdajdasda"); config.Setup(c => c["TokenInfo:Issuer"]).Returns("someissuer.com"); MedicalRepresentativeProvider provider = new MedicalRepresentativeProvider(mock.Object); AuthController controller = new AuthController(config.Object, provider); MedicalRepresentative obj = new MedicalRepresentative { Name = "Prithwiman", Email = "*****@*****.**", Password = "******" }; var data = controller.Post(obj); var result = data as ObjectResult; Assert.AreEqual(401, result.StatusCode); }
/// <summary> /// Checks if credentials provided by user are correct /// </summary> /// <param name="representative"></param> /// <returns>true for valid credentials and false for invalid credentials</returns> public bool Validate(MedicalRepresentative representative) { try { IEnumerable <MedicalRepresentative> representativesList = repository.GetMedicalRepresentatives(); MedicalRepresentative authRepresentative = representativesList.Where(r => r.Email == representative.Email).FirstOrDefault(); if (authRepresentative != null && authRepresentative.Password == representative.Password) { logger.Info("Successfully logged in " + authRepresentative.Name); return(true); } logger.Info("Invalid Credentials"); return(false); } catch (Exception e) { logger.Error("Exception arised in " + nameof(MedicalRepresentativeProvider) + "\n" + e.Message); throw e; } }