public void ValidSubscriberName()
        {
            //Arrange
            string notValidNameNumber  = "Igor Su3Sejid";
            string notValidNameSpecial = "Sonja#Stefanovska";
            string validName           = "Aleks Smith";
            bool   isNotValidNameNr;
            bool   isNotValidNameSp;
            bool   isValidName;

            //Act
            isNotValidNameNr = ValidationMetods.ValidSubscriberName(notValidNameNumber);
            isNotValidNameSp = ValidationMetods.ValidSubscriberName(notValidNameSpecial);
            isValidName      = ValidationMetods.ValidSubscriberName(validName);

            //Assert
            Assert.IsFalse(isNotValidNameNr, "Validation metod dasen't validate incorrect name format that contains numbers");
            Assert.IsFalse(isNotValidNameSp, "Validation metod dasen't validate incorrect name format that contains special char");
            Assert.IsTrue(isValidName, "Validation metod dasen't validate valid name format");
        }
Esempio n. 2
0
        public override void OnAuthorization(HttpActionContext actionContext)
        {
            if (actionContext.Request.Headers.Authorization == null)
            {
                actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized);
            }
            else
            {
                #region Decoding

                string   authenticationToken        = actionContext.Request.Headers.Authorization.Parameter;
                string   decodedAuthenticationToken = Encoding.UTF8.GetString(Convert.FromBase64String(authenticationToken));
                string[] userPassArray = decodedAuthenticationToken.Split(':');
                string   username      = userPassArray[0];
                string   password      = userPassArray[1];

                #endregion



                if (ValidationMetods.ValidSubscriberName(username))
                {
                    if (UserSecurity.LogIn(username, password))
                    {
                        Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(username), null);
                    }
                    else
                    {
                        actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized);
                    }
                }
                else
                {
                    actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized);
                }
            }
        }
Esempio n. 3
0
        public HttpResponseMessage Post([FromBody] TopArtistsSubscribers subscriber)
        {
            #region Create response messages
            responseMessage = new SubscribersResponseMessages
                              (
                subscriberMail: subscriber.SubscriberEmail,
                subscriberName: subscriber.SubscriberName
                              );
            #endregion

            bool isValidEmali = ValidationMetods.ValidSubscriberEmail(subscriber.SubscriberEmail);
            bool isValidName  = ValidationMetods.ValidSubscriberName(subscriber.SubscriberName);

            if (isValidEmali && isValidName)
            {
                try
                {
                    using (subscribersEntities = new SubscribersDBEntities())
                    {
                        var mailExsist = subscribersEntities.TopArtistsSubscribers.Any(
                            m => m.SubscriberEmail == subscriber.SubscriberEmail);
                        if (!mailExsist)
                        {
                            var newSubscriber = new TopArtistsSubscribers
                            {
                                Id                      = Guid.NewGuid(),
                                SubscriberName          = subscriber.SubscriberName.Trim(),
                                SubscriberEmail         = subscriber.SubscriberEmail.Trim(),
                                SubscribtionDateCreated = DateTime.UtcNow
                            };

                            subscribersEntities.TopArtistsSubscribers.Add(newSubscriber);
                            subscribersEntities.SaveChanges();

                            #region Send Email

                            //  var sender = ConfigurationManager.AppSettings.AllKeys.GetValue;

                            mailService = new SubscribersMailService
                                          (
                                recipientId: newSubscriber.Id.ToString(),
                                recipientName: newSubscriber.SubscriberName,
                                recipientMail: newSubscriber.SubscriberEmail
                                          );
                            mailService.MailSettings
                            (
                                sender: mailSettings.Sender,
                                password: mailSettings.Password,
                                smtpClient: mailSettings.SmtpClient,
                                smtpPort: mailSettings.SmtpPort,
                                mailSubject: mailSettings.Subject,
                                mailBodySubscribed: mailSettings.BodySubscribe,
                                mailBodyUnsubscribed: string.Empty
                            );
                            string mailSent = mailService.SendMail() == true?responseMessage.ResponseMailSubscribeSent()
                                                  : responseMessage.ErrorRresponseMail();

                            #endregion

                            return(Request.CreateResponse(HttpStatusCode.OK, responseMessage.ResponseCreated() + mailSent));
                        }
                        else
                        {
                            return(Request.CreateErrorResponse(HttpStatusCode.Conflict, responseMessage.ErrorResponseIsRegistrated()));
                        }
                    }
                }
                catch (Exception ex)
                {
                    responseMessage.Ex = ex.Message;
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, responseMessage.ErrorResponseEx()));
                }
            }
            else
            {
                if (!isValidEmali && !isValidName)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.Forbidden, responseMessage.ErrorResponseNotValidNameAndEmail()));
                }
                else if (!isValidEmali)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.Forbidden, responseMessage.ErrorResponseNotValidEmail()));
                }
                else
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.Forbidden, responseMessage.ErrorResponseNotValidName()));
                }
            }
        }