Example #1
0
        public void validatelogintest()
        {
            Data.Entities.HLContext _db  = new Data.Entities.HLContext();
            Data.UserRepository     test = new Data.UserRepository(_db);
            bool   expected = false;
            string username = "******";
            string password = "******";

            Domain.User user1 = new Domain.User();
            user1.UserFill(username, password);
            test.AddUser(user1);
            test.Save();
            var Users = test.GetUsers();

            foreach (var User in Users)
            {
                if (User.username == username)
                {
                    if (User.password == password)
                    {
                        expected = true;
                    }
                }
            }
            bool actual = test.validatelogin(username, password);

            Assert.AreEqual(expected, actual);
            test.DeleteUser(user1);
            test.Save();
        }
Example #2
0
        public void savetest()
        {
            Data.Entities.HLContext _db  = new Data.Entities.HLContext();
            Data.UserRepository     test = new Data.UserRepository(_db);
            bool   expected = false;
            string username = "******";
            string password = "******";

            Domain.User user1 = new Domain.User();
            user1.UserFill(username, password);
            test.AddUser(user1);
            test.Save();
            int userid = 0;
            var Users  = test.GetUsers();

            foreach (var User in Users)
            {
                if (User.username == username)
                {
                    userid   = User.id;
                    expected = true;
                }
            }

            bool actual = test.validateusername(test.GetUserByUserid(userid).username);

            Assert.AreEqual(expected, actual);
            test.DeleteUser(test.GetUserByUserid(userid));
            test.Save();
        }
Example #3
0
        public void getuserbyidtest()
        {
            Data.Entities.HLContext _db  = new Data.Entities.HLContext();
            Data.UserRepository     test = new Data.UserRepository(_db);
            string username = "******";
            string password = "******";
            string expected = username;
            int    userid   = 0;

            Domain.User user1 = new Domain.User(username, password);
            test.AddUser(user1);
            test.Save();
            var Users = test.GetUsers();

            foreach (var User in Users)
            {
                if (User.username == username)
                {
                    userid = User.id;
                }
            }
            string actual = test.GetUserByUserid(userid).username;

            Assert.AreEqual(expected, actual);
            test.DeleteUser(user1);
            test.Save();
        }
Example #4
0
        public void getuserstest()
        {
            Data.Entities.HLContext _db  = new Data.Entities.HLContext();
            Data.UserRepository     test = new Data.UserRepository(_db);
            string username1             = "Edwin";
            string password1             = "password";
            string username2             = "Push";
            string password2             = "Pinder";
            bool   expected = true;
            bool   actual   = false;

            Domain.User user1 = new Domain.User(username1, password1);
            Domain.User user2 = new Domain.User(username2, password2);
            test.AddUser(user1);
            test.AddUser(user2);
            test.Save();
            var Users = test.GetUsers();

            foreach (var User in Users)
            {
                actual = test.validateusername(User.username);
                if (actual == false)
                {
                    break;
                }
            }

            Assert.AreEqual(expected, actual);
            test.DeleteUser(user1);
            test.DeleteUser(user2);
            test.Save();
        }
        private static void CreateDatabaseSchemaAndDemoData()
        {
            CreateDatabaseSchema();

            var session = NHibernateHelper.SessionFactory.OpenSession();
            var passwordPolicy = new RegularExpressionPasswordPolicy(".{5,}$");
            var translationsRepository = new TranslationRepository(session, new InMemoryKeyValueCache());
            var applicationSettings = new ApplicationSettings();
            var encryptor = new DefaultEncryptor();

            var userRepository = new UserRepository(session, passwordPolicy, applicationSettings, encryptor);

            // Create administrators
            PocoGenerator.CreateAdministrators(userRepository);

            // Create users
            PocoGenerator.CreateUsers(100, userRepository);

            session.Transaction.Begin();

            // Create translations
            PocoGenerator.CreateTranslations(translationsRepository);

            session.Transaction.Commit();

            // Create logitems
            PocoGenerator.CreateLogItems(new NLogLogger(applicationSettings, "Console.Admin"));
        }
Example #6
0
        public void validateusernametest()
        {
            Data.Entities.HLContext _db  = new Data.Entities.HLContext();
            Data.UserRepository     test = new Data.UserRepository(_db);
            bool   expected = false;
            string username = "******";

            Domain.User user1 = new Domain.User();
            user1.UserFill("akash", "1234");
            test.AddUser(user1);
            test.Save();
            var Users = test.GetUsers();

            foreach (var User in Users)
            {
                if (User.username == username)
                {
                    expected = true;
                }
            }
            bool actual = test.validateusername(username);

            Assert.AreEqual(expected, actual);
            test.DeleteUser(user1);
            test.Save();
        }
        public HttpResponseMessage RegisterUser(UserModel model)
        {
            return this.ExecuteOperationAndHandleExceptions(() =>
            {
                this.ValidateUser(model);

                var data = new UserRepository(
                ConfigurationManager.AppSettings["MongoConnectionString"],
                ConfigurationManager.AppSettings["Database"]);

                var dbUser = data.All().FirstOrDefault(u => u.Username.ToLower() == model.Username.ToLower());
                if (dbUser != null)
                {
                    throw new InvalidOperationException("This user already exists in the database");
                }

                dbUser = new DbUserModel()
                {
                    Username = model.Username,
                    AuthCode = model.AuthCode
                };

                data.Add(dbUser);

                var responseModel = new RegisterUserResponseModel()
                {
                    Id = dbUser.Id,
                    Username = dbUser.Username,
                };

                var response = this.Request.CreateResponse(HttpStatusCode.Created, responseModel);
                return response;
            });
        }
        public HttpResponseMessage LoginUser(UserModel model)
        {
            return this.ExecuteOperationAndHandleExceptions(() =>
            {
                this.ValidateUser(model);

                var data = new UserRepository(
                ConfigurationManager.AppSettings["MongoConnectionString"],
                ConfigurationManager.AppSettings["Database"]);

                var dbUser = new DbUserModel()
                {
                    Username = model.Username,
                    AuthCode = model.AuthCode
                };

                var user = data.GetLoggedUser(dbUser);

                if (user == null)
                {
                    throw new FormatException("Invalid username or password");
                }

                string token = null;

                if (user.AccessToken == null)
                {
                    token = data.SetAccessToken(user, this.GenerateAccessToken(user.Id));
                }
                else
                {
                    token = user.AccessToken;
                }

                var responseModel = new LoginResponseModel()
                {
                    Id = user.Id,
                    Username = user.Username,
                    AccessToken = token
                };

                var response = this.Request.CreateResponse(HttpStatusCode.OK, responseModel);
                return response;
            });
        }
        public static List<User> CreateAdministrators(UserRepository userRepository)
        {
            var users = new List<User>();

            var robin = new User("*****@*****.**", "Robin van der Knaap", new CultureInfo("nl-NL"), userRepository, new ApplicationSettings());
            robin.Roles.Add(Role.Administrator);
            userRepository.Create(robin, "secret");
            users.Add(robin);

            var daan = new User("*****@*****.**", "Daan le Duc", new CultureInfo("nl-NL"), userRepository, new ApplicationSettings());
            daan.Roles.Add(Role.Administrator);
            userRepository.Create(daan, "secret");
            users.Add(daan);

            var johan = new User("*****@*****.**", "Johan van der Vleuten", new CultureInfo("nl-NL"), userRepository, new ApplicationSettings());
            johan.Roles.Add(Role.Administrator);
            userRepository.Create(johan, "secret");
            users.Add(johan);

            return users;
        }
Example #10
0
 public OrdersController(CategoryRepository categoryRepository,
     UserRepository userRepository, SubcategoryRepository subcategoryRepository,
     OrdersRepository ordersRepository, AssetRepository assetRepository, TaskRepository taskRepository,
     ReviewRepository reviewRepository, UserService userService,
     UserResponseHistoryRepository userResponseHistoryRepository,
     ViewCounterService viewCounterService,
     BillingService billingService, GigbucketDbContext dbContext)
 {
     _categoryRepository = categoryRepository;
     _userRepository = userRepository;
     _subcategoryRepository = subcategoryRepository;
     _ordersRepository = ordersRepository;
     _assetRepository = assetRepository;
     _taskRepository = taskRepository;
     _reviewRepository = reviewRepository;
     _userService = userService;
     _userResponseHistoryRepository = userResponseHistoryRepository;
     _viewCounterService = viewCounterService;
     _billingService = billingService;
     _dbContext = dbContext;
 }
        public static List<User> CreateUsers(int amount, UserRepository userRepository)
        {
            var users = new List<User>();

            foreach (var pocoUser in Session.List<PocoUser>(100).Get())
            {
                var user = new User(pocoUser.Email, pocoUser.DisplayName, new CultureInfo("nl-NL"), userRepository, new ApplicationSettings());
                user.Roles.Add(Role.User);
                userRepository.Create(user, "secret");
                users.Add(user);
            }

            return users;
        }
Example #12
0
        public void teamuserstest()
        {
            Data.Entities.HLContext _db   = new Data.Entities.HLContext();
            Data.UserRepository     test  = new Data.UserRepository(_db);
            Data.TeamRepository     test2 = new Data.TeamRepository(_db);
            string username = "******";
            string password = "******";

            Domain.User user1 = new Domain.User();
            user1.UserFill(username, password);
            string username2 = "Akash";
            string password2 = "other";

            Domain.User user2 = new Domain.User();
            user2.UserFill(username2, password2);
            bool actual1  = false;
            bool actual2  = false;
            bool expected = true;

            test.AddUser(user1);
            test.AddUser(user2);
            test.Save();

            var user1inteam2 = test.GetUserByUsername(username);
            var user2inteam2 = test.GetUserByUsername(username2);

            List <Domain.User> usersinteam1 = new List <Domain.User>();

            usersinteam1.Add(user1inteam2);
            usersinteam1.Add(user2inteam2);

            bool user1role = true;
            bool user2role = false;

            List <Boolean> team1roleslist = new List <Boolean>();

            team1roleslist.Add(user1role);
            team1roleslist.Add(user2role);

            Domain.Team team1 = new Domain.Team(usersinteam1, team1roleslist);
            team1.teamname = "Team1";

            test2.AddTeam(team1);
            _db.SaveChanges();
            var team1got = test2.GetByTeamName("Team1");

            Assert.AreEqual(team1got.teamname, "Team1");

            _db.SaveChanges();

            var usersinteamlist = test.TeamUsers(team1.teamname);

            foreach (var userinteamlist in usersinteamlist)
            {
                if (userinteamlist.username == username)
                {
                    actual1 = true;
                }
                if (userinteamlist.username == username2)
                {
                    actual2 = true;
                }
            }

            Assert.AreEqual(expected, actual1);
            Assert.AreEqual(expected, actual2);

            test.DeleteUser(user1inteam2);
            test.DeleteUser(user2inteam2);
            test2.DeleteTeam(team1got);
            test.Save();
            _db.SaveChanges();
        }
        public HttpResponseMessage LogoutUser(
            [ValueProvider(typeof(HeaderValueProviderFactory<string>))]
            string accessToken)
        {
            return this.ExecuteOperationAndHandleExceptions(() =>
            {
                var data = new UserRepository(
                ConfigurationManager.AppSettings["MongoConnectionString"],
                ConfigurationManager.AppSettings["Database"]);

                var user = this.GetUserByAccessToken(accessToken, data.Db);
                data.SetAccessToken(user, null);

                var response = this.Request.CreateResponse(HttpStatusCode.NoContent);
                return response;
            });
        }
Example #14
0
        public void UpdateTeamTest()
        {
            Data.Entities.HLContext _db      = new Data.Entities.HLContext();
            Data.TeamRepository     test     = new Data.TeamRepository(_db);
            Data.UserRepository     usertest = new Data.UserRepository(_db);

            //preliminary stuff to clean database in case this stuff is already in there
            //first see if the team used in this test is in the DB
            Data.Entities.Team isthisteamhere = _db.Team.Where(o => o.Teamname.Equals("testteamname")).FirstOrDefault();
            if (isthisteamhere != null)
            {
                //obtain the primary key for this team
                int primarykey = isthisteamhere.Id;
                //remove the userteam(s) associated with this team
                IEnumerable <UserTeam> ww = _db.UserTeam.Where(mm => mm.Teamid == primarykey);
                foreach (var item in ww)
                {
                    _db.UserTeam.Remove(item);
                }
                _db.SaveChanges();
                //now we can remove the team
                _db.Team.Remove(isthisteamhere);
                _db.SaveChanges();
            }

            //now we can remove our user1 and 2 if they exist
            Data.Entities.User isthisuserhere = _db.User.Where(p => p.Username.Equals("username1")).FirstOrDefault();
            if (isthisuserhere != null)
            {
                _db.User.Remove(isthisuserhere);
                _db.SaveChanges();
            }
            Data.Entities.User isthisuserhere2 = _db.User.Where(p => p.Username.Equals("username2")).FirstOrDefault();
            if (isthisuserhere2 != null)
            {
                _db.User.Remove(isthisuserhere2);
                _db.SaveChanges();
            }



            bool what;    //random bool to hold data about success of methods.
            bool success; //initialize boolean for asserts

            //first case, we pass the method a faulty team check for null case and count case.
            Domain.Team team = new Domain.Team();
            success = test.UpdateTeam(team);
            Assert.AreEqual(success, false);



            //second test case for we have an empty team in the database and it is updated to contain a team.
            team.teamname = "testteamname";

            Domain.User user = new Domain.User("username1", "password1");

            //need to add user to db and pull it to get a stupid id
            success = usertest.DeleteUser(user);
            usertest.Save();
            //add user to the database;
            success = usertest.AddUser(user);
            usertest.Save();
            if (!success)
            {
                Assert.Fail();
            }
            _db.SaveChanges();

            //obtain the user from the database so it has the userID
            var x = _db.User.Where(a => a.Username.Equals(user.username)).FirstOrDefault();

            Domain.User user1withID = Mapper.Map(x);

            team.Roles.Add(true);
            team.Userlist.Add(user1withID);

            what = test.DeleteTeam(team);
            what = test.AddTeam(team);

            //now I will add another user to the team and see if it updates.
            Domain.User user2 = new Domain.User("username2", "password2");
            usertest.AddUser(user2);
            usertest.Save();
            var xx = _db.User.Where(a => a.Username.Equals(user2.username)).FirstOrDefault();

            Domain.User user2withID = Mapper.Map(xx);
            team.AddMember(user2withID);

            success = test.UpdateTeam(team);
            Assert.AreEqual(success, true);
            //keep database clean and undo the things i put in it
            //first remove the userteams
            //preliminary stuff to clean database in case this stuff is already in there
            //first see if the team used in this test is in the DB
            Data.Entities.Team isthisteamhere3 = _db.Team.Where(o => o.Teamname.Equals("testteamname")).FirstOrDefault();
            if (isthisteamhere3 != null)
            {
                //obtain the primary key for this team
                int primarykey = isthisteamhere3.Id;
                //remove the userteam(s) associated with this team
                IEnumerable <UserTeam> ww = _db.UserTeam.Where(mm => mm.Teamid == primarykey);
                foreach (var item in ww)
                {
                    _db.UserTeam.Remove(item);
                }
                _db.SaveChanges();
                //now we can remove the team
                _db.Team.Remove(isthisteamhere3);
                _db.SaveChanges();
            }

            //now we can remove our user1 and 2 if they exist
            Data.Entities.User isthisuserhere3 = _db.User.Where(p => p.Username.Equals("username1")).FirstOrDefault();
            if (isthisuserhere3 != null)
            {
                _db.User.Remove(isthisuserhere3);
                _db.SaveChanges();
            }
            Data.Entities.User isthisuserhere4 = _db.User.Where(p => p.Username.Equals("username2")).FirstOrDefault();
            if (isthisuserhere4 != null)
            {
                _db.User.Remove(isthisuserhere4);
                _db.SaveChanges();
            }
        }
Example #15
0
        public void GetByTeamNameTest()
        {
            Data.Entities.HLContext _db      = new Data.Entities.HLContext();
            Data.TeamRepository     test     = new Data.TeamRepository(_db);
            Data.UserRepository     usertest = new Data.UserRepository(_db);
            bool success;



            Domain.Team miteam = new Domain.Team();
            miteam.teamname = "grisaia";
            Domain.User user1 = new Domain.User("username1", "password1");

            /*
             * //get team id for next query
             * Data.Entities.Team ii = _db.Team.Where(ss => ss.Teamname.Equals("grisaia")).FirstOrDefault();
             * //first remove all userteams associated with this team
             * IEnumerable<Data.Entities.UserTeam> grisaiausers = _db.UserTeam.Where(a => a.Teamid == ii.Id);
             * _db.SaveChanges();
             * if (grisaiausers.GetEnumerator()!=null)
             * {
             *  foreach (var item in grisaiausers)
             *  {
             *      _db.UserTeam.Remove(item);
             *
             *  }
             *  _db.SaveChanges();
             *
             *
             * }
             */

            //remove user from db if it exist
            success = usertest.DeleteUser(user1);
            usertest.Save();
            //add user to the database;
            success = usertest.AddUser(user1);
            usertest.Save();
            if (!success)
            {
                Assert.Fail();
            }
            _db.SaveChanges();

            //obtain the user from the database so it has the userID
            var x = _db.User.Where(a => a.Username.Equals(user1.username)).FirstOrDefault();

            Domain.User user1withID = Mapper.Map(x);

            miteam.Userlist.Add(user1withID);
            miteam.Roles.Add(true);

            //remove team from db if it exist
            success = test.DeleteTeam(miteam);
            //add team to database
            success = test.AddTeam(miteam);



            //obtain the team from the database so it has a teamID
            var y = _db.Team.Where(gg => gg.Teamname.Equals(miteam.teamname)).FirstOrDefault();

            Domain.Team miteamwithID = Mapper.Map(y);
            miteamwithID.Userlist.Add(user1withID);
            miteamwithID.Roles.Add(true);

            //create the userteam entity from the above.
            IEnumerable <Data.Entities.UserTeam> userteam = Mapper.Map(miteam).UserTeam;



            Domain.Team newteam = test.GetByTeamName("grisaia");



            Assert.AreEqual(newteam.Userlist.Count, miteam.Userlist.Count);
            Assert.AreEqual(newteam.Roles.Count, miteam.Roles.Count);

            //remove stuffs from database.


            //delete the userteam enetities in the database if they are already there
            foreach (var item in userteam)
            {
                var uu = _db.UserTeam.Where(gg => gg.Id == item.Id).FirstOrDefault();
                if (uu != null)
                {
                    _db.UserTeam.Remove(item);
                    _db.SaveChanges();
                }
            }

            success = test.DeleteTeam(miteamwithID);
            success = usertest.DeleteUser(user1withID);
        }
Example #16
0
        public void AddAndRemoveTeamTest()
        {
            Data.Entities.HLContext _db      = new Data.Entities.HLContext();
            Data.TeamRepository     test     = new Data.TeamRepository(_db);
            Data.UserRepository     usertest = new Data.UserRepository(_db);

            bool success; //variable to determine if a team was added or removed successfully.

            Domain.Team x = new Domain.Team();
            x.teamname = "XXstarstrikersXX1113452435x4";
            success    = test.DeleteTeam(x);
            _db.SaveChanges();
            success = test.AddTeam(x);
            _db.SaveChanges();
            //assert that the team was added to the database
            Assert.AreEqual(success, true);

            success = test.AddTeam(x);
            _db.SaveChanges();
            //assert that the team was not added to the database because it already exists
            Assert.AreEqual(success, false);

            //assert that the team was successfuly deleted from the database
            success = test.DeleteTeam(x);
            _db.SaveChanges();
            Assert.AreEqual(success, true);

            //assert that the team was not deleted from the database because it did not exist
            success = test.DeleteTeam(x);
            _db.SaveChanges();
            Assert.AreEqual(success, false);

            //assert that the propery userteam table was added to the database
            Domain.User anewuser = new Domain.User("newuser89", "newpassword89");
            //delete the user from DB incase it exist
            success = usertest.DeleteUser(anewuser);
            if (success)
            {
                usertest.Save();
            }
            //add the user to the DB
            success = usertest.AddUser(anewuser);
            if (success)
            {
                usertest.Save();
            }

            //pull the user from the database
            Domain.User anewuserwithID = usertest.GetUserByUsername("newuser89");
            //add the user to the team
            x.AddMember(anewuserwithID);
            //add the team to the DB
            success = test.DeleteTeam(x);
            _db.SaveChanges();
            success = test.AddTeam(x);
            _db.SaveChanges();

            //now check that a usertable was created properly for the team
            Data.Entities.UserTeam userteam = _db.UserTeam.Where(jj => jj.Userid == anewuserwithID.id).FirstOrDefault();

            Assert.AreEqual(userteam.Userid, anewuserwithID.id);

            //now remove the team from the db
            success = test.DeleteTeam(x);

            Data.Entities.UserTeam deleted = _db.UserTeam.Where(jj => jj.Userid == anewuserwithID.id).FirstOrDefault();
            //check that the userteam was deleted
            Assert.AreEqual(deleted, null);

            //delete the user from the DB to keep it clean
            usertest.DeleteUser(anewuserwithID);
            usertest.Save();
        }