def test_password_is_encrypted(self): import phpass phpass.PasswordHash.hash_password = self.hash_password_original payload = UserPayload().blend() response = payload.post() user = safe_query(self, User, 'id', response.id, 'id') self.assertNotEqual(user.password, payload['data']['attributes']['password']) self.assertEqual(user.password[:7], "$2a$11$")
def test_home_longitude_as_decimal_degrees(self, longitude, expected): response = UserPayload().blend()\ .set_longitude(longitude)\ .post(code=expected) if expected == 422: response.assertRaiseJsonApiError('/data/attributes/longitude') else: user = safe_query(self, User, 'id', response.id, 'id') self.assertEqual(user.longitude, Decimal(longitude))
def test_field_hour_is_integer(self, hour, expected): response = UserPayload().blend()\ .set_hour(hour)\ .post(code=expected) if expected == 422: response.assertRaiseJsonApiError('/data/attributes/hour') else: user = safe_query(self, User, 'id', response.id, 'id') self.assertEqual(user.hour, int(hour))
def test_field_observation_radius_is_integer(self, radius, expected): response = UserPayload().blend()\ .set_observation_radius(radius)\ .post(code=expected) if expected == 422: response.assertRaiseJsonApiError( '/data/attributes/observation-radius') else: user = safe_query(self, User, 'id', response.id, 'id') self.assertEqual(user.observation_radius, int(radius))
def test_field_daily_mails_is_boolean(self, value, expected_code, expected=False): response = UserPayload().blend()\ .set_daily_mails(value)\ .post(code=expected_code) if expected_code == 422: response.assertRaiseJsonApiError('/data/attributes/daily-mails') else: user = safe_query(self, User, 'id', response.id, 'id') self.assertEqual(user.daily_mails, expected)
def test_field_hour_default_to_random_if_none(self): response = UserPayload().blend()\ .set_hour(None)\ .post() user = safe_query(self, User, 'id', response.id, 'id') self.assertGreaterEqual(user.hour, 0) self.assertLessEqual(user.hour, 23)
def test_get_geokrety_details_via_include_dont_have_tracking_code(self): geokret = self.blend_geokret(type=GEOKRET_TYPE_HUMAN, owner=self.user_1) # Owner has acces to tracking code response = UserPayload()\ .get(self.user_1.id, args={'include': 'geokrety_owned'}, user=self.user_1) GeokretResponse(response['included'][0])\ .assertHasPublicAttributes(geokret)\ .assertHasTrackingCode(geokret.tracking_code) # Others don't have acces to tracking code response = UserPayload()\ .get(self.user_1.id, args={'include': 'geokrety_owned'}, user=self.user_2) GeokretResponse(response['included'][0])\ .assertHasPublicAttributes(geokret)\ .assertHasTrackingCode(None)
def test_user_details_has_public_attributes_as_anonymous_user(self): response = UserPayload()\ .get_collection()\ .assertCount(3) response.data[0].assertHasPublicAttributes(self.admin) response.data[1].assertHasPublicAttributes(self.user_1) response.data[2].assertHasPublicAttributes(self.user_2)
def test_user_details_has_private_attributes_as_admin(self): response = UserPayload()\ .get_collection(user=self.admin)\ .assertCount(3) response.data[0].assertHasPrivateAttributes(self.admin) response.data[1].assertHasPrivateAttributes(self.user_1) response.data[2].assertHasPrivateAttributes(self.user_2)
def test_field_hour_default_to_random_if_missing(self): response = UserPayload().blend()\ ._del_attribute('hour')\ .post() user = safe_query(self, User, 'id', response.id, 'id') self.assertGreaterEqual(user.hour, 0) self.assertLessEqual(user.hour, 23)
def test_field_username_cannot_be_blank(self, username, expected): response = UserPayload().blend()\ .set_name(username)\ .post()\ .assertHasAttribute('name', expected) user = safe_query(self, User, 'id', response.id, 'id') self.assertEqual(user.name, expected)
def test_pagination(self): self.blend_badge(count=3) response = UserPayload()\ .get_collection(args={'page[size]': '1'})\ .assertCount(6)\ .assertHasPaginationLinks() self.assertEqual(len(response['data']), 1) response.data[0].assertHasId(self.admin.id)
def test_user_details_via_news_author(self): news = self.blend_news(author=self.user_1) payload = UserPayload(_url="/v1/news/{}/author") payload.get(news.id, user=self.user_2)\ .assertHasPublicAttributes(news.author) payload.get(news.id, user=news.author)\ .assertHasPrivateAttributes(news.author) payload.get(666, user=news.author, code=404)\ .assertRaiseJsonApiError('news_id')
def test_user_details_via_news_subscription(self): news_subscription = self.blend_news_subscription(user=self.user_1) payload = UserPayload(_url="/v1/news-subscriptions/{}/user") payload.get(news_subscription.id, user=self.user_2)\ .assertHasPublicAttributes(news_subscription.user) payload.get(news_subscription.id, user=news_subscription.user)\ .assertHasPrivateAttributes(news_subscription.user) payload.get(666, user=news_subscription.user, code=404)\ .assertRaiseJsonApiError('news_subscription_id')
def test_user_details_via_move_author(self): move = self.blend_move(author=self.user_1) payload = UserPayload(_url="/v1/moves/{}/author") payload.get(move.id, user=self.user_2)\ .assertHasPublicAttributes(move.author) payload.get(move.id, user=move.author)\ .assertHasPrivateAttributes(move.author) payload.get(666, user=move.author, code=404)\ .assertRaiseJsonApiError('move_id')
def test_user_details_via_geokret_holder(self): geokret = self.blend_geokret(holder=self.user_1) payload = UserPayload(_url="/v1/geokrety/{}/holder") payload.get(geokret.id, user=self.user_2)\ .assertHasPublicAttributes(geokret.holder) payload.get(geokret.id, user=geokret.holder)\ .assertHasPrivateAttributes(geokret.holder) payload.get(666, user=geokret.holder, code=404)\ .assertRaiseJsonApiError('geokret_held_id')
def test_can_be_posted_as_anonymous(self): UserPayload()\ .set_name('someone')\ .set_email('*****@*****.**')\ .set_password('super secret passord')\ .set_language('pl')\ .set_country('pl')\ .post(user=None)\ .assertHasAttribute('country', 'pl')\ .assertHasAttribute('language', 'pl')\ .assertHasAttribute('name', 'someone')\ .assertDateTimePresent('join-datetime')\
def test_user_details_via_move_comment(self): move_comment = self.blend_move_comment() payload = UserPayload(_url="/v1/moves-comments/{}/author") payload.get(move_comment.id)\ .assertHasPublicAttributes(move_comment.author) payload.get(666, code=404)\ .assertRaiseJsonApiError('move_comment_id')
def test_languages_are_checked_against_static_list(self, language, expected): response = UserPayload().blend()\ .set_language(language)\ .post(code=expected) if expected == 422: response.assertRaiseJsonApiError('/data/attributes/language') else: response.assertHasAttribute('language', language)
def test_countries_are_checked_against_static_list(self, country, expected): response = UserPayload().blend()\ .set_country(country)\ .post(code=expected) if expected == 422: response.assertRaiseJsonApiError('/data/attributes/country') else: response.assertHasAttribute('country', country)
def test_user_details_has_relationships_data(self): news = self.blend_news(author=self.user_1, count=2) news_comments = self.blend_news_comment(author=self.user_1, count=2) news_subscriptions = self.blend_news_subscription(user=self.user_1, count=2) geokrety_owned = self.blend_geokret(owner=self.user_1, count=2) geokrety_held = self.blend_geokret(holder=self.user_1, type=MOVE_TYPE_GRABBED, count=2) moves = self.blend_move(author=self.user_1, type=MOVE_TYPE_DROPPED, count=2) UserPayload()\ .get(self.user_1.id, user=self.admin, args={ 'include': 'news,news_comments,news_subscriptions,geokrety_owned,geokrety_held,moves' })\ .assertHasRelationshipNewsDatas(news)\ .assertHasRelationshipNewsCommentsDatas(news_comments)\ .assertHasRelationshipNewsSubscriptionsAuthorDatas(news_subscriptions)\ .assertHasRelationshipGeokretyOwnedDatas(geokrety_owned)\ .assertHasRelationshipGeokretyHeldDatas(geokrety_held)\ .assertHasRelationshipMovesDatas(moves)
def test_user_geokrety_held_relationship(self): geokret = self.blend_geokret(holder=self.user_1) UserPayload(_url_collection="/v1/users/{}/relationships/geokrety-held".format(self.user_1.id))\ .get_collection()\ .assertHasDatas('geokret', [geokret.id])
def test_user_news_subscriptions_relationship(self): news_subscription = self.blend_news_subscription(user=self.user_1) UserPayload(_url_collection="/v1/users/{}/relationships/news-subscriptions".format(self.user_1.id))\ .get_collection()\ .assertHasDatas('news-subscription', [news_subscription.id])
def test_user_news_comments_relationship(self): news_comment = self.blend_news_comment(author=self.user_1) UserPayload(_url_collection="/v1/users/{}/relationships/news-comments".format(self.user_1.id))\ .get_collection()\ .assertHasDatas('news-comment', [news_comment.id])
def test_user_move_relationship(self): move = self.blend_move(author=self.user_1) UserPayload(_url_collection="/v1/users/{}/relationships/moves".format(self.user_1.id))\ .get_collection()\ .assertHasDatas('move', [move.id])
def test_backend_generate_join_date_time(self): response = UserPayload().blend()\ ._del_attribute('join-datetime')\ .post() user = safe_query(self, User, 'id', response.id, 'id') user.join_datetime.strftime("%Y-%m-%dT%H:%M:%S")
def test_backend_generate_secid(self): payload = UserPayload().blend() response = payload.post() user = safe_query(self, User, 'id', response.id, 'id') self.assertNotEqual(unicode(payload['data']['attributes']['secid']), user.secid)
def test_optionnal_fields(self, attribute): UserPayload().blend()\ ._del_attribute(attribute)\ .post()
def test_unicity_field_email(self): UserPayload().blend().set_email('*****@*****.**').post() UserPayload().blend().set_email('*****@*****.**').post(code=422)\ .assertJsonApiErrorCount(1)\ .assertRaiseJsonApiError('/data/attributes/email')
def test_unicity_field_username(self): UserPayload().blend().set_name('someone').post() UserPayload().blend().set_name('someone').post(code=422)\ .assertJsonApiErrorCount(1)\ .assertRaiseJsonApiError('/data/attributes/name')