def test_should_read_all_users(self): # given user_1 = UserEntity() user_1.user_name = "test_user_1" user_1.created_on = datetime.utcnow() user_1.modified_on = datetime.utcnow() user_1.opt_lock = 0 user_2 = UserEntity() user_2.user_name = "test_user_2" user_2.created_on = datetime.utcnow() user_2.modified_on = datetime.utcnow() user_2.opt_lock = 0 self.session.add(user_1) self.session.add(user_2) self.session.flush() # when found_users = user_repository.read_all() # then self.assertIsNotNone(found_users) self.assertIn(user_1, found_users) self.assertIn(user_2, found_users)
def test_should_get_all_campaigns_of_given_gm(self): # given user = UserEntity() user.user_name = "test_user" user.created_on = datetime.utcnow() user.modified_on = datetime.utcnow() user.opt_lock = 0 campaign_1 = CampaignEntity() campaign_1.campaign_name = "campaign_1" campaign_1.created_on = datetime.utcnow() campaign_1.modified_on = datetime.utcnow() campaign_1.opt_lock = 0 campaign_1.begin_date = date(2055, 1, 1) campaign_1.passed_days = 0 campaign_1.game_master = user campaign_2 = CampaignEntity() campaign_2.campaign_name = "campaign_2" campaign_2.created_on = datetime.utcnow() campaign_2.modified_on = datetime.utcnow() campaign_2.opt_lock = 0 campaign_2.begin_date = date(2055, 1, 1) campaign_2.passed_days = 0 campaign_2.game_master = user other_user = UserEntity() other_user.user_name = "other_user" other_user.created_on = datetime.utcnow() other_user.modified_on = datetime.utcnow() other_user.opt_lock = 0 campaign_3 = CampaignEntity() campaign_3.campaign_name = "campaign_3" campaign_3.created_on = datetime.utcnow() campaign_3.modified_on = datetime.utcnow() campaign_3.opt_lock = 0 campaign_3.begin_date = date(2055, 1, 1) campaign_3.passed_days = 0 campaign_3.game_master = other_user self.session.add(campaign_1) self.session.add(campaign_2) self.session.add(campaign_3) self.session.flush() # when found_campaigns = campaign_repository.read_by_gm_id(user.id) # then self.assertIsNotNone(found_campaigns) self.assertIn(campaign_1, found_campaigns) self.assertIn(campaign_2, found_campaigns) self.assertNotIn(campaign_3, found_campaigns)
def test_should_persist_new_campaign(self): # given user = UserEntity() user.user_name = "test_user" user.created_on = datetime.utcnow() user.modified_on = datetime.utcnow() user.opt_lock = 0 self.session.add(user) self.session.flush() new_campaign = CampaignEntity() new_campaign.created_on = datetime.utcnow() new_campaign.modified_on = datetime.utcnow() new_campaign.opt_lock = 0 new_campaign.campaign_name = "test campaign" new_campaign.begin_date = date(2054, 1, 1) new_campaign.passed_days = 0 new_campaign.game_master_id = user.id # when campaign_repository.persist(new_campaign) # then self.assertIn(new_campaign, self.session.query(CampaignEntity).all())
def test_should_get_campaign(self): # given user = UserEntity() user.user_name = "Test User" user.created_on = datetime.utcnow() user.modified_on = datetime.utcnow() user.opt_lock = 0 campaign = CampaignEntity() campaign.campaign_name = "Test Campaign" campaign.created_on = datetime.utcnow() campaign.modified_on = datetime.utcnow() campaign.opt_lock = 0 campaign.begin_date = date(2055, 1, 1) campaign.passed_days = 0 campaign.game_master = user self.session.add(campaign) self.session.flush() # when found_campaign = campaign_repository.read_by_id(campaign.id) # then self.assertEqual(campaign, found_campaign)
def test_should_get_all_user_roles(self): # given user = UserEntity() user.id = 1 user.user_name = "test_user" role_1 = UserRoleEntity() role_1.id = 1 role_1.role_name = "test_role_1" role_1.user = user role_2 = UserRoleEntity() role_2.id = 2 role_2.role_name = "test_role_2" role_2.user = user self.user_repository_mock.read_by_user_name.return_value = user # when found_roles = user_roles_service.get_user_roles("test_user") # then self.assertIsNotNone(found_roles) self.assertEqual(2, len(found_roles)) self.assertIn("test_role_1", found_roles) self.assertIn("test_role_2", found_roles)
def test_should_persist_new_user_with_history_entry(self): # given user = UserEntity() user.user_name = "test_user" user.created_on = datetime.utcnow() user.modified_on = datetime.utcnow() user.opt_lock = 0 user_history_entry = UserHistoryEntity() user_history_entry.created_on = datetime.utcnow() user_history_entry.modified_on = datetime.utcnow() user_history_entry.opt_lock = 0 user_history_entry.editor_name = "test" user_history_entry.message = "test message" user_history_entry.user = user # when user_repository.persist(user) # then self.assertIsNotNone( self.session.query(UserEntity).filter_by( user_name="test_user").first()) self.assertIsNotNone( self.session.query(UserHistoryEntity).filter_by(user=user).first())
def test_should_get_user_by_user_name(self): # given user = UserEntity() user.id = 1 user.user_name = "test_user" user.created_on = datetime.utcnow() user.modified_on = datetime.utcnow() user.opt_lock = 0 user.password_hash = "test_hash" self.user_repository_mock.read_by_user_name.return_value = user # when user_data = user_service.get_user_by_user_name("test_user") # then self.assertIsNotNone(user_data) self.assertIsInstance(user_data, dict) self.assertEqual(user.id, user_data["id"]) self.assertEqual(user.user_name, user_data["user_name"]) self.assertEqual(user.created_on, user_data["created_on"]) self.assertEqual(user.modified_on, user_data["modified_on"]) self.assertEqual(user.opt_lock, user_data["opt_lock"]) self.assertEqual(user.password_hash, user_data["password_hash"]) self.user_repository_mock.read_by_user_name.assert_called_once_with( "test_user")
def test_should_get_false_when_user_is_not_related_to_campaign(self): # given campaign = CampaignEntity() campaign.id = 99 campaign.created_on = date(2020, 1, 1) campaign.modified_on = date(2020, 1, 1) campaign.opt_lock = 0 campaign.campaign_name = "Test Campaign" campaign.begin_date = date(2055, 1, 1) campaign.passed_days = 10 gm = UserEntity() gm.id = 77 gm.created_on = date(2019, 1, 1) gm.modified_on = date(2019, 1, 1) gm.opt_lock = 0 gm.user_name = "Test User" campaign.game_master = gm self.campaign_repository_mock.read_by_id.return_value = campaign # when result = campaign_service.check_if_user_related_to_campaign(88, 99) # then self.assertEqual(False, result)
def test_should_get_campaign_details(self): # given campaign = CampaignEntity() campaign.id = 99 campaign.created_on = datetime(2020, 1, 1, 12, 12, 12) campaign.modified_on = datetime(2020, 1, 1, 13, 13, 13) campaign.opt_lock = 0 campaign.campaign_name = "Test Campaign" campaign.begin_date = date(2055, 1, 1) campaign.passed_days = 10 gm = UserEntity() gm.id = 88 gm.created_on = date(2019, 1, 1) gm.modified_on = date(2019, 1, 1) gm.opt_lock = 0 gm.user_name = "Test User" campaign.game_master = gm self.campaign_repository_mock.read_by_id.return_value = campaign # when campaign_details = campaign_service.get_campaign_details(99) # then self.assertEqual(99, campaign_details["id"]) self.assertEqual("Test Campaign", campaign_details["title"]) self.assertEqual("2020-01-01T12:12:12", campaign_details["creationDate"]) self.assertEqual("2055-01-01", campaign_details["startDate"]) self.assertEqual(10, campaign_details["passedDays"]) self.assertEqual("2055-01-11", campaign_details["currentDate"]) self.assertEqual(88, campaign_details["gameMasterId"]) self.assertEqual("Test User", campaign_details["gameMasterName"])
def _create_user_entity(user_name, password, timestamp): user = UserEntity() user.user_name = user_name user.password_hash = security.generate_password_hash(password) user.created_on = timestamp user.modified_on = timestamp user.opt_lock = 0 return user
def test_should_raise_error_when_user_already_exists(self): # given user = UserEntity() user.id = 1 user.user_name = "test_user" self.user_repository_mock.read_by_user_name.return_value = user with self.assertRaises(BusinessError): user_service.create_user("test_editor", "test_user", "test_password")
def test_should_persist_new_user(self): # given user = UserEntity() user.user_name = "test_user" user.created_on = datetime.utcnow() user.modified_on = datetime.utcnow() user.opt_lock = 0 # when user_repository.persist(user) # then self.assertIn(user, self.session.query(UserEntity).all())
def test_should_get_all_users(self): # given user_1 = UserEntity() user_1.user_name = "test_user_1" user_1.created_on = datetime.utcnow() user_1.modified_on = datetime.utcnow() user_1.opt_lock = 0 user_2 = UserEntity() user_2.user_name = "test_user_2" user_2.created_on = datetime.utcnow() user_2.modified_on = datetime.utcnow() user_2.opt_lock = 0 self.user_repository_mock.read_all.return_value = [user_1, user_2] # when found_users = user_service.get_all_users() # then self.assertIsNotNone(found_users) self.assertEqual(2, len(found_users)) self.assertEqual("test_user_1", found_users[0]["user_name"]) self.assertEqual("test_user_2", found_users[1]["user_name"])
def test_should_grant_new_roles_to_user(self): # given test_user = UserEntity() test_user.user_name = "test_user" self.user_repository_mock.read_by_user_name.return_value = test_user # when user_roles_service.grant_roles("test_editor", "test_user", ["test_role_1", "test_role_2"]) # then self.user_repository_mock.persist.assert_called_once_with(test_user) self.assertEqual(2, len(test_user.user_roles)) self.assertEqual("test_role_1", test_user.user_roles[0].role_name) self.assertEqual("test_role_2", test_user.user_roles[1].role_name)
def test_should_get_user_by_user_name(self): # given user = UserEntity() user.user_name = "test_user" user.created_on = datetime.utcnow() user.modified_on = datetime.utcnow() user.opt_lock = 0 self.session.add(user) self.session.flush() # when found_user = user_repository.read_by_user_name("test_user") # then self.assertIsNotNone(found_user) self.assertEqual(user, found_user)
def test_should_get_nothing_when_gm_has_no_campaigns(self): # given user = UserEntity() user.user_name = "test_user" user.created_on = datetime.utcnow() user.modified_on = datetime.utcnow() user.opt_lock = 0 self.session.add(user) self.session.flush() # when found_campaigns = campaign_repository.read_by_gm_id(user.id) # then self.assertIsNotNone(found_campaigns) self.assertEqual(0, len(found_campaigns))
def test_should_update_user(self): # given user = UserEntity() user.user_name = "test_user" user.created_on = datetime.utcnow() user.modified_on = datetime.utcnow() user.opt_lock = 0 self.session.add(user) self.session.flush() # when user.password_hash = "test_hash" user_repository.persist(user) # then self.assertEqual( "test_hash", self.session.query(UserEntity).filter_by( user_name="test_user").first().password_hash)
def test_should_not_modify_old_role_when_granting_new_one(self): # given test_user = UserEntity() test_user.user_name = "test_user" old_role = UserRoleEntity() old_role.role_name = "old_role" old_role.created_on = datetime(2010, 1, 1) old_role.user = test_user self.user_repository_mock.read_by_user_name.return_value = test_user # when user_roles_service.grant_roles("test_editor", "test_user", ["old_role", "new_role"]) # then self.assertEqual(2, len(test_user.user_roles)) self.assertEqual("old_role", test_user.user_roles[0].role_name) self.assertEqual(datetime(2010, 1, 1), test_user.user_roles[0].created_on)
def test_should_persist_new_campaign_with_timeline_entry(self): # given user = UserEntity() user.user_name = "test_user" user.created_on = datetime.utcnow() user.modified_on = datetime.utcnow() user.opt_lock = 0 self.session.add(user) self.session.flush() new_campaign = CampaignEntity() new_campaign.created_on = datetime.utcnow() new_campaign.modified_on = datetime.utcnow() new_campaign.opt_lock = 0 new_campaign.campaign_name = "test campaign" new_campaign.begin_date = date(2054, 1, 1) new_campaign.passed_days = 0 new_campaign.game_master_id = user.id new_campaign_timeline_entry = CampaignTimelineEntity() new_campaign_timeline_entry.created_on = datetime.utcnow() new_campaign_timeline_entry.modified_on = datetime.utcnow() new_campaign_timeline_entry.opt_lock = 0 new_campaign_timeline_entry.title = "test session" new_campaign_timeline_entry.session_date = date.today() new_campaign_timeline_entry.summary_text = "this is summary" new_campaign_timeline_entry.campaign = new_campaign # when campaign_repository.persist(new_campaign) # then self.assertIn(new_campaign, self.session.query(CampaignEntity).all()) self.assertIsNotNone( self.session.query(CampaignTimelineEntity).filter_by( campaign_id=new_campaign.id).first())
def test_should_persist_new_user_with_role(self): # given user = UserEntity() user.user_name = "test_user" user.created_on = datetime.utcnow() user.modified_on = datetime.utcnow() user.opt_lock = 0 user_role = UserRoleEntity() user_role.created_on = datetime.utcnow() user_role.modified_on = datetime.utcnow() user_role.opt_lock = 0 user_role.role_name = "test_role" user_role.user = user # when user_repository.persist(user) # then self.assertIsNotNone( self.session.query(UserEntity).filter_by( user_name="test_user").first()) self.assertIsNotNone( self.session.query(UserRoleEntity).filter_by(user=user).first)