public void TestAlias()
        {
            var clientService = new ClientService();

            //Create a different user
            CreateUser("Jimminy", "Cricket", "*****@*****.**", 10);

            var result = clientService.CheckAlias(new LoginData()
            {
                Username = username,
                Password = password,
                Alias    = "NoobStick"
            });
            Assert.AreEqual(CheckAliasResult.Available, result);

            result = clientService.CheckAlias(new LoginData()
            {
                Username = username,
                Password = password,
                Alias    = "Jimminy"
            });
            Assert.AreEqual(CheckAliasResult.Unavailable, result);

            result = clientService.CheckAlias(new LoginData()
            {
                Username = username,
                Password = password,
                Alias    = username
            });
            Assert.AreEqual(CheckAliasResult.Registered, result);
        }
        public void TestCheckForUpdates()
        {
            int lobbyId = 0;
            using (var db = new CSSDataContext())
            {
                //Create autoupdate files
                var file = new AutoUpdateFile() { Filename = "mdl", IsProtected = true };
                db.AutoUpdateFiles.InsertOnSubmit(file);

                var lobby   = db.Lobbies.FirstOrDefault();
                lobbyId     = lobby.Id;

                file.AutoUpdateFile_Lobbies.Add(new AutoUpdateFile_Lobby()
                {
                    CurrentVersion  = "1.0",
                    DateCreated     = DateTime.Now,
                    DateModified    = DateTime.Now,
                    Lobby           = lobby,
                    ValidChecksum   = "VALID"
                });

                db.SubmitChanges();
            }

            var clientService   = new ClientService();
            var files           = clientService.CheckForUpdates(lobbyId);
            var fileResult      = files.Files.FirstOrDefault();

            Assert.AreEqual(1, files.Files.Count);
            Assert.IsTrue(fileResult.IsProtected);
            Assert.AreEqual("mdl", fileResult.Filename);
            Assert.AreEqual("VALID", fileResult.ValidChecksum.Trim());
            Assert.AreEqual("1.0", fileResult.CurrentVersion);
            Assert.AreEqual(lobbyId, fileResult.LobbyId);
        }
        public void TestRetrieveMessages()
        {
            Initialize();

            var logins = new string[,] { { "One", "1" }, { "Two", "2" }, { "Three", "3" }, { "Four", "4" } };

            //Try for each login to check that group messaging works as intended
            for (int i = 0; i <= logins.GetUpperBound(0); i++)
            {
                var service = new ClientService();

                ListMessageResult messagesFull = service.ListMessages(new AuthenticatedData()
                {
                    Username = logins[i, 0],
                    Password = logins[i, 1]
                });

                Assert.AreEqual(3, messagesFull.Messages.Count);

                //Check again, should be no new messages
                ListMessageResult messagesEmpty = service.ListMessages(new AuthenticatedData()
                {
                    Username = logins[i,0],
                    Password = logins[i,1]
                });

                Assert.AreEqual(0, messagesEmpty.Messages.Count);
            }
        }
        public void TestBan()
        {
            DataAccess.BanType banType = Initialize();

            //Test Set, ListBans, Remove, ensure user has requisite permissions.
            var adminService = new Administration();
            var result = adminService.SetBan(new BanData()
            {
                BanMode				= BanMode.Auto,
                BanTypeId			= banType.Id,
                Username            = "******",
                Password            = "******",
                Reason              = "#1 Orion is being a general dick.",
                Alias               = "Orion",
            });

            Assert.IsTrue(result);

            //Ensure banned user can no longer log in
            var clientService = new ClientService();
            var loginResult = clientService.Login(new LoginData()
            {
                Username    = "******",
                Password    = "******",
                Alias       = "Orion"
            });
            Assert.AreEqual(LoginStatus.AccountLocked, loginResult.Status);

            //List bans
            var bans = adminService.ListBans(new AuthenticatedData()
            {
                Username = "******",
                Password = "******"
            }, "Orion");

            //Remove all bans
            var ban = bans.FirstOrDefault();
            adminService.RemoveBan(new BanData()
            {
                Username    = "******",
                Password    = "******",
                BanId       = ban.Id
            });

            bans = adminService.ListBans(new AuthenticatedData()
            {
                Username = "******",
                Password = "******"
            }, "Orion");

            Assert.AreEqual(0, bans.Where(p => p.InEffect == true).Count());

            //Ensure user can log in.
            clientService = new ClientService();
            loginResult = clientService.Login(new LoginData()
            {
                Username    = "******",
                Password    = "******",
                Alias       = "Orion",
                LobbyId     = 1
            });
            Assert.AreEqual(LoginStatus.Authenticated, loginResult.Status);

            //Set two sequential bans.
            result = adminService.SetBan(new BanData()
            {
                BanMode             = BanMode.Auto,
                Username            = "******",
                Password            = "******",
                BanTypeId			= banType.Id,
                Reason              = "#2 Orion is being a general dick.",
                Alias               = "Orion",
            });
            Assert.IsTrue(result);

            result = adminService.SetBan(new BanData()
            {
                BanMode             = BanMode.Auto,
                Username            = "******",
                Password            = "******",
                BanTypeId			= banType.Id,
                Reason              = "#3 Orion is /STILL/ being a general dick.",
                Alias               = "Orion",
            });
            Assert.IsTrue(result);

            //Retrieve ban
            bans = adminService.ListBans(new AuthenticatedData()
            {
                Username = "******",
                Password = "******"
            }, "Orion");

            Assert.AreEqual(2, bans.Where(p => p.InEffect == true).Count());

            var firstBan	= bans.Where(p => p.InEffect == true).OrderBy(p => p.DateCreated).First();
            var lastBan		= bans.Where(p => p.InEffect == true).OrderBy(p => p.DateCreated).Last();

            var firstDuration   = (firstBan.DateExpires.Value - firstBan.DateCreated).TotalMinutes;
            var lastDuration    = (lastBan.DateExpires.Value - lastBan.DateCreated).TotalMinutes;

            Assert.IsTrue(lastDuration > firstDuration);

            //Reset ban length
            adminService.SetBan(new BanData()
            {
                Username    = "******",
                Password    = "******",
                BanId       = firstBan.Id,
                BanMode     = BanMode.Custom,
                Duration    = TimeSpan.MinValue,
                Alias		= "Orion"
            });

            adminService.SetBan(new BanData()
            {
                Username    = "******",
                Password    = "******",
                BanId       = lastBan.Id,
                BanMode     = BanMode.Custom,
                Duration    = TimeSpan.MinValue,
                Alias		= "Orion"
            });

            bans = adminService.ListBans(new AuthenticatedData()
            {
                Username = "******",
                Password = "******"
            }, "Orion");

            Assert.AreEqual(2, bans.Where(p => p.InEffect == true).Count());

            Assert.AreEqual(0, bans.Where(p => p.InEffect == true && p.DateExpires > DateTime.Now).Count());
        }
        public void TestListAliases()
        {
            Initialize();

            var adminService = new Administration();
            var results = adminService.ListAliases(new AuthenticatedData()
            {
                Username = "******",
                Password = "******"
            }, "Admin");
            Assert.AreEqual(1, results.Count);

            // Try to log in with a non-existant alias.
            var clientService = new ClientService();
            var loginResult = clientService.Login(new LoginData()
            {
                Username = "******",
                Password = "******",
                Alias    = "Jerky",
                LobbyId  = 1
            });
            Assert.AreEqual(LoginStatus.Authenticated, loginResult.Status);
            Assert.AreEqual("Admin", loginResult.AcceptedAlias, "Logging in with an invalid alias reverts back to the oldest available alias.");

            results = adminService.ListAliases(new AuthenticatedData()
            {
                Username = "******",
                Password = "******"
            }, "Admin");
            Assert.AreEqual(1, results.Count);
        }
        public void TestDefaultAlias()
        {
            var clientService = new ClientService();

            Login createdLogin = CreateUser("MultiAlias", "Alias", "*****@*****.**", 10);

            using(CSSDataContext db = new CSSDataContext())
            {
                var login = db.Logins.FirstOrDefault(p => p.Id == createdLogin.Id);
                login.Aliases.Add(new Alias()
                {
                    Callsign = "alias1",
                    IsActive = true,
                    DateCreated = DateTime.Now
                });

                login.Aliases.Add(new Alias()
                {
                    Callsign = "alias2",
                    IsActive = true,
                    DateCreated = DateTime.Now
                });

                login.Aliases.Add(new Alias()
                {
                    Callsign = "alias3",
                    IsActive = true,
                    DateCreated = DateTime.Now
                });

                db.SubmitChanges();

                login = db.Logins.FirstOrDefault(p => p.Id == createdLogin.Id);

                Assert.AreEqual(4, login.Aliases.Count);
                Assert.IsTrue(login.Aliases.FirstOrDefault(p => p.Callsign == "MultiAlias").IsDefault);
                Assert.AreEqual(1, login.Aliases.Where(p => p.IsDefault == true).Count());

                var alias2 = login.Aliases.FirstOrDefault(p => p.Callsign == "alias2");

                // This should fail because the credentials are wrong.
                clientService.SetDefaultAlias(new SetDefaultAliasData()
                {
                    AliasId = alias2.Id,
                    Username = username,
                    Password = password
                });

                Assert.IsFalse(login.Aliases.FirstOrDefault(p => p.Callsign == "alias2").IsDefault);
                Assert.AreEqual(1, login.Aliases.Where(p => p.IsDefault == true).Count());

                // This one should work.
                clientService.SetDefaultAlias(new SetDefaultAliasData()
                {
                    AliasId = alias2.Id,
                    Username = "******",
                    Password = "******"
                });

                Assert.IsFalse(login.Aliases.FirstOrDefault(p => p.Callsign == "alias2").IsDefault);
                Assert.AreEqual(1, login.Aliases.Where(p => p.IsDefault == true).Count());
            }
        }
        public void TestLogin()
        {
            var clientService = new ClientService();

            var result = clientService.Login(new LoginData()
            {
                Username    = username,
                Password    = password,
                Alias       = username,
                LobbyId     = 1 //Production
            });
            Assert.AreEqual(LoginStatus.Authenticated, result.Status);

            result = clientService.Login(new LoginData()
            {
                Username    = "******",
                Password    = "******",
                Alias       = "BogusAlias"
            });
            Assert.AreEqual(LoginStatus.InvalidCredentials, result.Status);

            result = clientService.Login(new LoginData()
            {
                Username    = username,
                Password    = password,
                Alias       = "NewAlias",
                LobbyId     = 1
            });
            Assert.AreEqual(LoginStatus.Authenticated, result.Status);

            result = clientService.Login(new LoginData()
            {
                Username    = username,
                Password    = password,
                Alias       = "Invalid Alias",
                LobbyId     = 1
            });
            Assert.AreEqual(LoginStatus.Authenticated, result.Status);
            Assert.AreEqual(username, result.AcceptedAlias, "Logging in with an invalid alias but a valid account will revert back to the oldest alias for that account.");
        }
        public void TestLegacyAlias()
        {
            var clientService = new ClientService();

            // acsstest1/acsstest12

            //Create a different user
            CreateUser("acsstest_mainaccount", "acsstest12", "*****@*****.**", 10);

            // ASGS callsigns and passwords match; this user can have this alias.
            var result = clientService.CheckAlias(new LoginData()
            {
                Username = "******",
                Password = "******",
                Alias = "acsstest1"
            });

            Assert.AreEqual(CheckAliasResult.Available, result);

            CreateUser("acsstest_mainacct2", "acsstest12_nope", "*****@*****.**", 20);

            // ASGS callsigns match, invalid password; this user can't have this alias.
            result = clientService.CheckAlias(new LoginData()
            {
                Username = "******",
                Password = "******",
                Alias = "acsstest1"
            });

            Assert.AreEqual(CheckAliasResult.InvalidLegacyPassword, result);

            // ASGS callsign doesn't match, this user can have this alias.
            result = clientService.CheckAlias(new LoginData()
            {
                Username = username,
                Password = password,
                Alias = "acsstest1xx"
            });

            Assert.AreEqual(CheckAliasResult.Available, result);

            //result = clientService.CheckAlias(new LoginData()
            //{
            //    Username = username,
            //    Password = password,
            //    Alias = "aarmstrong"
            //});

            //Assert.AreEqual(CheckAliasResult.InvalidLegacyPassword, result);
        }
        public void TestPolls()
        {
            Initialize();

            var logins = new string[,] {{"One","1"},{"Two","2"},{"Three","3"},{"Four","4"}};

            for (int i = 0; i <= logins.GetUpperBound(0); i++)
            {
                var messaging = new ClientService();
                var polls = messaging.ListPolls(new AuthenticatedData()
                {
                    Username = logins[i,0],
                    Password = logins[i,1]
                });
                Assert.AreEqual(2, polls.Count);

                PollData vote = new PollData()
                {
                    //Each user votes for their option %3 (result being 1st option has 2 votes, other 2 have 1 vote)
                    OptionId = polls[0].PollOptions[(Convert.ToInt16(logins[i,1])-1)%3].Id,
                    Username = logins[i,0],
                    Password = logins[i,1]
                };
                messaging.ApplyVote(vote);
                messaging.ApplyVote(vote); //Send the vote again, prevent anyone from voting more than once.
            }

            using (var db = new CSSDataContext())
            {
                Assert.AreEqual(2, db.PollVotes.Count(pv => pv.PollOption.Option == "Option1"));
                Assert.AreEqual(1, db.PollVotes.Count(pv => pv.PollOption.Option == "Option2"));
            }
        }