예제 #1
0
    def test_office_admin_add(self):
        form = {
            "siret": "78548035101646",
            "company_name": "SUPERMARCHES MATCH",
            "office_name": "SUPERMARCHES MATCH",
            "naf": "4711D",
            "street_number": "45",
            "street_name": "AVENUE ANDRE MALRAUX",
            "city_code": "57463",
            "zipcode": "57000",
            "email": "*****@*****.**",
            "tel": "0387787878",
            "website": "http://www.supermarchesmatch.fr",
            "flag_alternance": 0,
            "flag_junior": 0,
            "flag_senior": 0,
            "flag_handicap": 0,
            "departement": "57",
            "headcount": "12",
            "score": 90,
            "score_alternance": 75,
            "x": 6.17952,
            "y": 49.1044,
            "reason": "Demande de mise en avant",
        }

        with self.test_request_context():
            # Create an user admin
            self.user = User(email='*****@*****.**', gender='male',
                             first_name='John', last_name='Doe', active=True,
                             is_admin=True)
            db_session.add(self.user)
            db_session.flush()

            user_social_auth = UserSocialAuth(
                provider=PEAMOpenIdConnect.name,
                extra_data={'id_token': 'fake'},
                user_id=self.user.id,
            )
            db_session.add(user_social_auth)
            db_session.commit()

            # Login as user admin
            self.user = db_session.query(User).filter_by(id=self.user.id).first()
            self.assertEqual(db_session.query(User).count(), 1)
            self.login(self.user)

            # Create OfficeAdminRemove
            self.assertEqual(0, OfficeAdminAdd.query.filter_by(id=1).count())
            self.app.post(url_for('officeadminadd.create_view'), data=form)
            self.assertEqual(1, OfficeAdminAdd.query.filter_by(id=1).count())

            # Delete OfficeAdminAdd
            self.app.post(url_for('officeadminadd.delete_view'), data={'id': 1})
            self.assertEqual(0, OfficeAdminRemove.query.filter_by(id=1).count())
예제 #2
0
 def get_geocode_from_csv(self, csv_api_path):
     logger.info("Parsing CSV sent back by API : {}".format(csv_api_path))
     df_geocodes = pd.read_csv(csv_api_path, dtype={'siret': str})
     for index, row in df_geocodes.iterrows():
         if not numpy.isnan(row.latitude):
             coordinates = [row.longitude, row.latitude]
             geolocation = Geolocation.get(row.full_address)
             # There should not be an already existing geolocation
             # but working on this job, makes you know that sometimes,
             # the coordinates related to a siret do not update, but the geolocation is still added
             # in the database
             if geolocation:
                 logger.info("Geolocation already found")
                 GEOCODING_STATS['updatable_coordinates'] = GEOCODING_STATS.get(
                     'updatable_coordinates', 0) + 1
                 coordinates_updates.append(
                     [row.siret, coordinates])
             else:
                 logger.info("Geolocation not found")
                 geolocation = Geolocation(
                     full_address=row.full_address,
                     x=coordinates[0],
                     y=coordinates[1]
                 )
                 db_session.add(geolocation)
                 # as this method is run in parallel jobs,
                 # let's commit often so that each job see each other's changes
                 # and rollback in case of rare simultaneous changes on same geolocation
                 try:
                     db_session.commit()
                     # usually flush() is called as part of commit()
                     # however it is not the case in our project
                     # because autoflush=False
                     db_session.flush()
                     GEOCODING_STATS['flushes'] = GEOCODING_STATS.get(
                         'flushes', 0) + 1
                 except IntegrityError:
                     # happens when a job tries to insert an already existing full_address
                     # rollback needed otherwise db_session is left
                     # in a state unusable by the other parallel jobs
                     db_session.rollback()
                     GEOCODING_STATS['rollbacks'] = GEOCODING_STATS.get(
                         'rollbacks', 0) + 1
                 if coordinates:
                     GEOCODING_STATS['updatable_coordinates'] = GEOCODING_STATS.get(
                         'updatable_coordinates', 0) + 1
                     coordinates_updates.append(
                         [row.siret, coordinates])
         else:
             GEOCODING_STATS['coordinates_not_found'] = GEOCODING_STATS.get(
                 'coordinates_not_found', 0) + 1
예제 #3
0
    def test_office_admin_remove(self):
        # Create officeAdminRemove
        form = {
            'siret': '01234567891234',
            'name': 'Test company',
            'reason': 'N/A',
            'initiative': 'office',
        }

        with self.test_request_context():
            # Create an user admin
            self.user = User(email='*****@*****.**',
                             gender='male',
                             first_name='John',
                             last_name='Doe',
                             active=True,
                             is_admin=True)
            db_session.add(self.user)
            db_session.flush()

            user_social_auth = UserSocialAuth(
                provider=PEAMOpenIdConnect.name,
                extra_data={'id_token': 'fake'},
                user_id=self.user.id,
            )
            db_session.add(user_social_auth)
            db_session.commit()

            # Login as user admin
            self.user = db_session.query(User).filter_by(
                id=self.user.id).first()
            self.assertEqual(db_session.query(User).count(), 1)
            self.login(self.user)

            # Create OfficeAdminRemove
            self.assertEqual(
                0,
                OfficeAdminRemove.query.filter_by(
                    siret='01234567891234').count())
            self.app.post(url_for('officeadminremove.create_view'), data=form)
            self.assertEqual(
                1,
                OfficeAdminRemove.query.filter_by(
                    siret='01234567891234').count())

            # Delete OfficeAdminRemove
            self.app.post(url_for('officeadminremove.delete_view'),
                          data={'id': 1})
            self.assertEqual(0,
                             OfficeAdminRemove.query.filter_by(id=1).count())
예제 #4
0
    def test_get_user_social_auth(self):
        """
        Test the `get_user_social_auth()` function.
        """
        user = User(email='*****@*****.**', gender='male', first_name='John', last_name='Doe')
        db_session.add(user)
        db_session.flush()

        expected_user_social_auth = UserSocialAuth(provider=PEAMOpenIdConnect.name, extra_data=None, user_id=user.id)
        db_session.add(expected_user_social_auth)
        db_session.flush()

        db_session.commit()

        self.assertEqual(db_session.query(User).count(), 1)
        self.assertEqual(db_session.query(UserSocialAuth).count(), 1)

        user_social_auth = get_user_social_auth(user.id)
        self.assertEqual(user_social_auth.id, expected_user_social_auth.id)
예제 #5
0
    def test_logout(self):
        """
        Test that the session is cleaned after a logout.
        """

        user = User(email='*****@*****.**',
                    gender='male',
                    first_name='John',
                    last_name='Doe')
        db_session.add(user)
        db_session.flush()

        # This `UserSocialAuth` entry will be required later by the logout function.
        user_social_auth = UserSocialAuth(
            provider=PEAMOpenIdConnect.name,
            extra_data={'id_token': 'fake'},
            user_id=user.id,
        )
        db_session.add(user_social_auth)
        db_session.commit()

        with self.test_request_context:

            with self.app.session_transaction() as sess:
                sess[
                    'this_should_not_be_deleted'] = 'foo'  # This should not be deleted by a login or logout.

            self.login(user)

            with self.app.session_transaction() as sess:
                self.assertIn('this_should_not_be_deleted', sess)
                self.assertIn('user_id', sess)
                self.assertIn('social_auth_last_login_backend', sess)
                self.assertIn('peam-openidconnect_state', sess)

            self.logout()

            with self.app.session_transaction() as sess:
                self.assertIn('this_should_not_be_deleted', sess)
                self.assertNotIn('user_id', sess)
                self.assertNotIn('social_auth_last_login_backend', sess)
                self.assertNotIn('peam-openidconnect_state', sess)
예제 #6
0
    def setUp(self, *args, **kwargs):
        super(AdminTest, self).setUp(*args, **kwargs)

        self.user = User(email='*****@*****.**',
                         gender='male',
                         first_name='John',
                         last_name='Doe')
        db_session.add(self.user)
        db_session.flush()

        # Required for `self.logout` to work which looks for the `extra_data` attribute.
        user_social_auth = UserSocialAuth(
            provider=PEAMOpenIdConnect.name,
            extra_data={'id_token': 'fake'},
            user_id=self.user.id,
        )
        db_session.add(user_social_auth)
        db_session.commit()

        self.assertEqual(db_session.query(User).count(), 1)
    def setUp(self):
        """
        Populate the DB with data required for these tests to work.
        """
        super(UserAccountTest, self).setUp()

        self.user = User(email='*****@*****.**',
                         gender='male',
                         first_name='John',
                         last_name='Doe')
        db_session.add(self.user)
        db_session.flush()

        self.office1 = Office(
            departement='57',
            siret='00000000000001',
            company_name='1',
            headcount='5',
            city_code='57070',
            zipcode='57070',
            naf='4646Z',
            score=90,
            x=6.166667,
            y=49.133333,
        )
        self.office2 = Office(
            departement='57',
            siret='00000000000002',
            company_name='1',
            headcount='5',
            city_code='57070',
            zipcode='57070',
            naf='4646Z',
            score=90,
            x=6.166667,
            y=49.133333,
        )
        db_session.add_all([self.office1, self.office2])
        db_session.flush()

        self.user_social_auth = UserSocialAuth(
            provider=PEAMOpenIdConnect.name,
            extra_data={'id_token': 'fake'},
            user_id=self.user.id,
        )
        self.fav1 = UserFavoriteOffice(user_id=self.user.id,
                                       office_siret=self.office1.siret)
        self.fav2 = UserFavoriteOffice(user_id=self.user.id,
                                       office_siret=self.office2.siret)
        db_session.add_all([self.user_social_auth, self.fav1, self.fav2])
        db_session.flush()

        db_session.commit()

        self.assertEqual(db_session.query(User).count(), 1)
        self.assertEqual(db_session.query(Office).count(), 2)
        self.assertEqual(db_session.query(UserFavoriteOffice).count(), 2)
        self.assertEqual(db_session.query(UserSocialAuth).count(), 1)
예제 #8
0
    def find_coordinates_for_address(self):
        """
        finding coordinates for an address based on the BAN (base d'adresses nationale),
        an online governmental service.
        """
        coordinates = None
        # FIXME refer to settings.API_ADRESS_BASE_URL and make sure we don't
        # make real requests in unit tests
        BASE = "http://api-adresse.data.gouv.fr/search/?q="
        geocoding_request = "%s%s" % (BASE, self.full_address)
        geolocation = Geolocation.get(self.full_address)

        if geolocation:
            # coordinates were already queried and cached before
            coordinates = [geolocation.x, geolocation.y]
            GEOCODING_STATS['cache_hits'] = GEOCODING_STATS.get(
                'cache_hits', 0) + 1
        else:
            # coordinates need to be queried and cached
            response = session.get(geocoding_request)
            response.close()
            GEOCODING_STATS['cache_misses'] = GEOCODING_STATS.get(
                'cache_misses', 0) + 1
            if response.status_code == 200:
                try:
                    results = response.json()['features']
                    if len(results) >= 1:
                        coordinates = results[0]['geometry']['coordinates']
                        # let's cache the result for later computations
                        geolocation = Geolocation(
                            full_address=self.full_address,
                            x=coordinates[0],
                            y=coordinates[1])
                        db_session.add(geolocation)

                        # as this method is run in parallel jobs,
                        # let's commit often so that each job see each other's changes
                        # and rollback in case of rare simultaneous changes on same geolocation
                        try:
                            db_session.commit()
                            # usually flush() is called as part of commit()
                            # however it is not the case in our project
                            # because autoflush=False
                            db_session.flush()
                            GEOCODING_STATS['flushes'] = GEOCODING_STATS.get(
                                'flushes', 0) + 1
                        except IntegrityError:
                            # happens when a job tries to insert an already existing full_address
                            # rollback needed otherwise db_session is left
                            # in a state unusable by the other parallel jobs
                            db_session.rollback()
                            GEOCODING_STATS['rollbacks'] = GEOCODING_STATS.get(
                                'rollbacks', 0) + 1
                except ValueError:
                    logger.warning('ValueError in json-ing features result %s',
                                   response.text)

        if coordinates:
            if coordinates == self.initial_coordinates:
                GEOCODING_STATS['unchanged_coordinates'] = GEOCODING_STATS.get(
                    'unchanged_coordinates', 0) + 1
            else:
                GEOCODING_STATS['updatable_coordinates'] = GEOCODING_STATS.get(
                    'updatable_coordinates', 0) + 1
                self.updates.append([self.siret, coordinates])
        else:
            GEOCODING_STATS['coordinates_not_found'] = GEOCODING_STATS.get(
                'coordinates_not_found', 0) + 1