def test_save_fails_if_integrity_error(self): """Test save raises a DBIntegrityError if the instance to be saved lacks a required value""" user = UserFactory.build(name=None) assert_raises(DBIntegrityError, self.user_repo.save, user)
def test_ask_user_to_subscribe_returns_false_if_newsletter_prompted(self): nw = Newsletter() app = MagicMock() nw.init_app(app) user = UserFactory.build(newsletter_prompted=True) assert nw.ask_user_to_subscribe(user) is False
def test_ask_user_to_subscribe_returns_false_if_newsletter_prompted(self, mailchimp): nw = Newsletter() app = MagicMock() nw.init_app(app) user = UserFactory.build(newsletter_prompted=True) assert nw.ask_user_to_subscribe(user) is False
def test_save(self): """Test save persist the user""" user = UserFactory.build() assert self.user_repo.get(user.id) is None self.user_repo.save(user) assert self.user_repo.get(user.id) == user, "User not saved"
def test_ask_user_to_subscribe_returns_true_if_user_not_subscribed(self, mailchimp): user = UserFactory.build(newsletter_prompted=False) app = MagicMock() fake_client = MagicMock() fake_client.lists.member_info.return_value = {"success_count": 0, "data": [{"email": "*****@*****.**"}]} mailchimp.Mailchimp.return_value = fake_client nw = Newsletter() nw.init_app(app) assert nw.ask_user_to_subscribe(user) is True
def test_login_fail_without_signup(api_client): user_data = UserFactory.build() login_url = '/api/users/login/' user_data = { 'email': user_data.email, 'password': user_data.password, } response = api_client.post(login_url, user_data) assert response.status_code == 400
def test_ask_user_to_subscribe_returns_false_if_user_subscribed(self, req_mock): user = UserFactory.build(newsletter_prompted=False) app = MagicMock() nw = Newsletter() nw.init_app(app) req_mock.side_effect = [FakeResponse(text=json.dumps(dict(foo='bar')), json=lambda: dict(status=200), status_code=200)] assert nw.ask_user_to_subscribe(user) is False
def test_ask_user_to_subscribe_returns_true_if_user_not_subscribed(self, mailchimp): user = UserFactory.build(newsletter_prompted=False) app = MagicMock() fake_client = MagicMock() fake_client.lists.member_info.return_value = { 'success_count': 0, 'data': [{'email':'*****@*****.**'}]} mailchimp.Mailchimp.return_value = fake_client nw = Newsletter() nw.init_app(app) assert nw.ask_user_to_subscribe(user) is True
def test_ask_user_to_subscribe_returns_false_if_user_subscribed(self, mailchimp): user = UserFactory.build(newsletter_prompted=False) app = MagicMock() fake_client = MagicMock() fake_client.lists.member_info.return_value = { 'success_count': 1, 'data': [{'email':user.email_addr}]} mailchimp.Mailchimp.return_value = fake_client nw = Newsletter() nw.init_app(app) assert nw.ask_user_to_subscribe(user) is False
def test_trimp_activities(mocker): user_a = UserFactory.build() user_b = UserFactory.build() all_activities = MockSet() mocker.patch.object(models.Activity, 'objects', all_activities) all_activities.add( MockModel(owner=user_b, trimp=100, time=datetime.datetime(2017, 5, 4, 3, 2, 1)), MockModel(owner=user_a, trimp=100, time=datetime.datetime(2017, 5, 5, 4, 2, 1)), MockModel(owner=user_a, trimp=None, time=datetime.datetime(2017, 5, 6, 5, 2, 1)), MockModel(owner=user_a, trimp=100, time=datetime.datetime(2017, 5, 7, 6, 2, 1)), MockModel(owner=user_a, trimp=100, time=datetime.datetime(2017, 5, 8, 6, 2, 1)), MockModel(owner=user_a, trimp=100, time=datetime.datetime(2017, 5, 9, 7, 2, 1)), MockModel(owner=user_b, trimp=100, time=datetime.datetime(2017, 5, 10, 8, 2, 1)), ) activities, start, end = models.Activity.trimp_activities(user_a) assert set(a.time.day for a in activities) == {5, 7, 8, 9} assert start.day == 5 assert end.day == 9 activities, start, end = models.Activity.trimp_activities( user_a, start=datetime.datetime(2017, 5, 5, 10, 5, 5), end=datetime.datetime(2017, 5, 8, 10, 5, 5)) assert set(a.time.day for a in activities) == {7, 8} assert start.day == 5 assert end.day == 8
def signup(api_client): user_data = UserFactory.build() credentials = { 'email': user_data.email, 'name': user_data.name, 'password': user_data.password, 're_password': user_data.password } signup_url = "/api/djoser/users/" user_created = api_client.post(signup_url, credentials) user = SignupResponseOrCredentials(response=user_created, credentials=credentials) return user
def test_create_user_endpoint_succeeds1(self): location = LocationFactory() create_user_role('create_user') user = UserFactory.build() user.save() role = RoleFactory() user_data = dict(firstName=user.first_name, lastName=user.last_name, roleId=role.id) headers = self.headers() headers.update({'X-Location': location.id}) response = self.client().post(self.make_url("/users/"), headers=headers, data=self.encode_to_json_string(user_data)) response_json = self.decode_from_json_string(response.data.decode('utf-8')) self.assertEqual(response.status_code, 201) self.assertEqual(response_json['msg'], 'OK') self.assertEqual(response_json['payload']['user']['firstName'], user.first_name) self.assertEqual(response_json['payload']['user']['lastName'], user.last_name)
def test_create_user_endpoint_succeeds(self): create_user_role('create_user') user = UserFactory.build() user_data = dict(firstName=user.first_name, lastName=user.last_name) response = self.client().post( self.make_url("/users/"), headers=self.headers(), data=self.encode_to_json_string(user_data)) response_json = self.decode_from_json_string( response.data.decode('utf-8')) self.assertEqual(response.status_code, 201) self.assertEqual(response_json['msg'], 'OK') self.assertEqual(response_json['payload']['user']['firstName'], user.first_name) self.assertEqual(response_json['payload']['user']['lastName'], user.last_name)
def test_ask_user_to_subscribe_returns_false_if_not_initialized(self): nw = Newsletter() user = UserFactory.build() assert nw.ask_user_to_subscribe(user) is False
def create_user(db, **kwargs): user = UserFactory.build(**kwargs) db.session.add(user) db.session.commit() return user
def setUp(self): self.user = UserFactory.build( username='******' ) # create a user but don't save until later in the mock super().setUp()