public void NotAdd_AnExistingHost()
        {
            //ARRANGE
            DateTime DateOfBirth = new DateTime(1990, 06, 01);
            Host     newHost     = new Host("Oktay", "Çelik", DateOfBirth);
            HostCollection <Host> HostCollection = new HostCollection <Host>();

            HostCollection.Add(newHost);
            int Expected = HostCollection.Length;

            //ACT
            HostCollection.Add(newHost);
            int Actual = HostCollection.Length;

            //ASSERT
            Assert.AreEqual(Expected, Actual);
        }
        public void GetHostAt_GivenIndex0()
        {
            //ARRANGE
            DateTime DateOfBirth = new DateTime(1990, 06, 01);
            Host     newHost     = new Host("Oktay", "Çelik", DateOfBirth);
            HostCollection <Host> HostCollection = new HostCollection <Host>();

            HostCollection.Add(newHost);
            Host Expected = newHost;
            //ACT
            Host Actual = HostCollection.GetHostAt(0);

            //ASSERT
            Assert.AreEqual(Expected, Actual);
        }
示例#3
0
        /// <param name="name">User name. Must be unique. Cannot be null or empty.</param>
        /// <param name="pass">Password. Cannot be null or empty.</param>
        /// <param name="email">Email address. Must be unique if specified. Cannot be null.</param>
        /// <param name="hostname">Host name where the registration is from.</param>
        /// <param name="timeSpan">Time in which the user filled the registration form.</param>
        /// <param name="softbannedIPs">List of application's soft-banned IPs. Soft-banned IPs are cleared when the application restarts.</param>
        /// <param name="verifyEmailUrl">Email verification URL. Cannot be null.</param>
        /// <returns>Data contract for the created user. Cannot be null.</returns>
        /// <exception cref="InvalidEmailFormatException">If the email format was invalid.</exception>
        /// <exception cref="UserNameAlreadyExistsException">If the user name was already taken.</exception>
        /// <exception cref="UserEmailAlreadyExistsException">If the email address was already taken.</exception>
        /// <exception cref="TooFastRegistrationException">If the user registered too fast.</exception>
        public UserContract Create(string name, string pass, string email, string hostname, TimeSpan timeSpan,
                                   HostCollection softbannedIPs, string verifyEmailUrl)
        {
            ParamIs.NotNullOrEmpty(() => name);
            ParamIs.NotNullOrEmpty(() => pass);
            ParamIs.NotNull(() => email);

            if (timeSpan < TimeSpan.FromSeconds(5))
            {
                log.Warn(string.Format("Suspicious registration form fill time ({0}) from {1}.", timeSpan, hostname));

                if (timeSpan < TimeSpan.FromSeconds(2))
                {
                    softbannedIPs.Add(hostname);
                }

                throw new TooFastRegistrationException();
            }

            return(repository.HandleTransaction(ctx => {
                // Verification
                var lc = name.ToLowerInvariant();
                var existing = ctx.Query().FirstOrDefault(u => u.NameLC == lc);

                if (existing != null)
                {
                    throw new UserNameAlreadyExistsException();
                }

                if (!string.IsNullOrEmpty(email))
                {
                    ValidateEmail(email);

                    existing = ctx.Query().FirstOrDefault(u => u.Active && u.Email == email);

                    if (existing != null)
                    {
                        throw new UserEmailAlreadyExistsException();
                    }
                }

                // All ok, create user
                var sfsCheckResult = sfsClient.CallApi(hostname);
                var sfsStr = GetSFSCheckStr(sfsCheckResult);

                var salt = new Random().Next();
                var hashed = LoginManager.GetHashedPass(lc, pass, salt);
                var user = new User(name, hashed, email, salt);
                user.UpdateLastLogin(hostname);
                ctx.Save(user);

                if (sfsCheckResult != null && sfsCheckResult.Conclusion == SFSCheckResultType.Malicious)
                {
                    var report = new UserReport(user, UserReportType.MaliciousIP, null, hostname,
                                                string.Format("Confidence {0} %, Frequency {1}, Last seen {2}.",
                                                              sfsCheckResult.Confidence, sfsCheckResult.Frequency, sfsCheckResult.LastSeen.ToShortDateString()));

                    ctx.OfType <UserReport>().Save(report);

                    user.GroupId = UserGroupId.Limited;
                    ctx.Update(user);
                }

                if (!string.IsNullOrEmpty(user.Email))
                {
                    var subject = "Welcome to VocaDB, please verify your email.";
                    SendEmailVerificationRequest(ctx, user, verifyEmailUrl, subject);
                }

                ctx.AuditLogger.AuditLog(string.Format("registered from {0} in {1} (SFS check {2}).", MakeGeoIpToolLink(hostname), timeSpan, sfsStr), user);

                return new UserContract(user);
            }));
        }