def test_user_is_pro(self): """ Test that the Pro user is correctly detected in various cases. Note : A pro user can be defined by his IP address too but it's not possible to test. So, only tests by email are provided """ user_pro = User.create(email='*****@*****.**', gender='male', first_name='John', last_name='Doe') user_public = User.create(email='*****@*****.**', gender='male', first_name='John', last_name='Doe') with self.test_request_context: # User which is not logged in should not be considered a pro user. self.assertFalse(pro.user_is_pro()) # User with a pro email should be considered as a pro user. self.login(user_pro) self.assertTrue(pro.user_is_pro()) self.logout() self.assertFalse(pro.user_is_pro()) # User with a non pro email should not be considered a pro user. self.login(user_public) self.assertFalse(pro.user_is_pro()) self.logout() self.assertFalse(pro.user_is_pro())
def test_user_is_pro(self): """ Test that the Pro Version is correctly detected in various cases. """ user_pro = User.create(email=u'*****@*****.**', gender=u'male', first_name=u'John', last_name=u'Doe') user_public = User.create(email=u'*****@*****.**', gender=u'male', first_name=u'John', last_name=u'Doe')
def login_as_pro(self): user_pro = User.create(email='*****@*****.**', gender='male', first_name='John', last_name='Doe') self.login(user_pro) self.assertTrue(pro.user_is_pro()) self.assertFalse(pro.pro_version_enabled())
def setUp(self): super().setUp() self.pro_user = User.create( email='*****@*****.**', gender='male', first_name='John', last_name='Doe', ) self.public_user = User.create( email='*****@*****.**', gender='male', first_name='John', last_name='Doe', ) user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:52.0) Gecko/20100101 Firefox/52.0" allowed_ip = settings.VERSION_PRO_ALLOWED_IPS[ 1] # IP address // should not be a range self.headers = { 'X-Forwarded-For': allowed_ip, 'User_Agent': user_agent, }
def test_enable_disable_pro_version_view(self): """ Test that the Pro Version is correctly enabled/disabled. """ # Create a user. user_pro = User.create(email='*****@*****.**', gender='male', first_name='John', last_name='Doe') next_url_without_domain = '/entreprises/metz-57000/boucherie?sort=score&d=10&h=1&p=0&f_a=0' next_url_with_domain = 'http://labonneboite.pole-emploi.fr' + next_url_without_domain url = self.url_for('user.pro_version', **{'next': next_url_without_domain}) with self.test_request_context: # Log the user in. self.login(user_pro) self.assertTrue(pro.user_is_pro()) self.assertFalse(pro.pro_version_enabled()) with self.app.session_transaction() as sess: self.assertNotIn(pro.PRO_VERSION_SESSION_KEY, sess) # Enable pro version. rv = self.app.get(url) self.assertEqual(rv.status_code, 302) self.assertEqual(rv.location, next_url_with_domain) # It is unclear what is the root cause of the following test # failure. The session object manipulated in the # pro_version_enabled function is different from the session # provided by the self.app.session_transaction context manager, but # I don't know why. # self.assertTrue(pro.pro_version_enabled()) with self.app.session_transaction() as sess: self.assertIn(pro.PRO_VERSION_SESSION_KEY, sess) self.assertEqual(True, sess[pro.PRO_VERSION_SESSION_KEY]) # Disable pro version. rv = self.app.get(url) self.assertEqual(rv.status_code, 302) self.assertEqual(rv.location, next_url_with_domain) self.assertFalse(pro.pro_version_enabled()) with self.app.session_transaction() as sess: self.assertNotIn(pro.PRO_VERSION_SESSION_KEY, sess)
def setUp(self, *args, **kwargs): super(FavoriteBaseTest, self).setUp(*args, **kwargs) # Create a user. self.user = User.create(email=u'*****@*****.**', gender=u'male', first_name=u'John', last_name=u'Doe') # Delete index. self.es.indices.delete(index=self.ES_TEST_INDEX) # Create new index. request_body = { "mappings": { "office": { "properties": { "naf": { "type": "string", "index": "not_analyzed" }, "siret": { "type": "string", "index": "not_analyzed" }, "name": { "type": "string", "index": "not_analyzed" }, "score": { "type": "integer", "index": "not_analyzed" }, "headcount": { "type": "integer", "index": "not_analyzed" }, "location": { "type": "geo_point", } } } } } self.es.indices.create(index=self.ES_TEST_INDEX, body=request_body) # Insert test data into Elasticsearch. docs = [ { 'naf': u'4711F', 'siret': u'00000000000001', 'score': 98, 'headcount': 32, 'location': self.positions['metz']['location'], 'name': u'Centre Distributeur E.Leclerc', }, { 'naf': u'4722Z', 'siret': u'00000000000002', 'score': 98, 'headcount': 12, 'location': self.positions['metz']['location'], 'name': u'Maison Nicolas', }, { 'naf': u'4711F', 'siret': u'00000000000003', 'score': 98, 'headcount': 32, 'location': self.positions['paris']['location'], 'name': u'Carrefour Paris', }, { 'naf': u'4722Z', 'siret': u'00000000000004', 'score': 98, 'headcount': 84, 'location': self.positions['paris']['location'], 'name': u'Maistre Mathieu', }, ] for i, doc in enumerate(docs, start=1): self.es.index(index=self.ES_TEST_INDEX, doc_type=self.ES_OFFICE_TYPE, id=i, body=doc) # Sleep required by ES to register new documents, flaky test here otherwise. time.sleep(1) # Create related Office instances into MariaDB/MySQL. for doc in docs: # Set the right `commune_id` and `zipcode` depending on the location. commune_id = None zip_code = None commune_id = None for position in self.positions: if doc['location'] == self.positions[position]['location']: commune_id = self.positions[position]['commune_id'] zip_code = self.positions[position]['zip_code'] break if not commune_id: raise ValueError("Cannot create an entry in Office with a city absent from self.positions.") office = Office( company_name=doc['name'], siret=doc['siret'], score=doc['score'], naf=doc['naf'], city_code=commune_id, zipcode=zip_code, email=u'*****@*****.**', departement=zip_code[:2], x=doc['location']['lon'], y=doc['location']['lat'], ) office.save() # We should have as much entries in MariaDB/MySQL than in Elasticsearch. self.assertEquals(Office.query.count(), len(docs))
def setUp(self): super(FavoriteBaseTest, self).setUp() # Create a user. self.user = User.create(email='*****@*****.**', gender='male', first_name='John', last_name='Doe') # Insert test data into Elasticsearch. docs = [ { 'naf': '4711F', 'siret': '00000000000001', 'score': 98, 'headcount': 32, 'locations': self.positions['metz']['coords'], 'name': 'Centre Distributeur E.Leclerc', }, { 'naf': '4722Z', 'siret': '00000000000002', 'score': 98, 'headcount': 12, 'locations': self.positions['metz']['coords'], 'name': 'Maison Nicolas', }, { 'naf': '4711F', 'siret': '00000000000003', 'score': 98, 'headcount': 32, 'locations': self.positions['paris']['coords'], 'name': 'Carrefour Paris', }, { 'naf': '4722Z', 'siret': '00000000000004', 'score': 98, 'headcount': 84, 'locations': self.positions['paris']['coords'], 'name': 'Maistre Mathieu', }, ] for i, doc in enumerate(docs, start=1): self.es.index(index=settings.ES_INDEX, doc_type=es.OFFICE_TYPE, id=i, body=doc) # Required by ES to register new documents, flaky test here otherwise. self.es.indices.flush(index=settings.ES_INDEX) # Create related Office instances into MariaDB/MySQL. for doc in docs: # Set the right `commune_id` and `zipcode` depending on the location. commune_id = None zip_code = None commune_id = None for position in self.positions: if doc['locations'] == self.positions[position]['coords']: commune_id = self.positions[position]['commune_id'] zip_code = self.positions[position]['zip_code'] break if not commune_id: raise ValueError( "Cannot create an entry in Office with a city absent from self.positions." ) office = Office( company_name=doc['name'], siret=doc['siret'], score=doc['score'], naf=doc['naf'], city_code=commune_id, zipcode=zip_code, email='*****@*****.**', departement=zip_code[:2], x=doc['locations'][0]['lon'], y=doc['locations'][0]['lat'], ) office.save() # We should have as much entries in MariaDB/MySQL than in Elasticsearch. self.assertEqual(Office.query.count(), len(docs))