Exemplo n.º 1
0
    def test_api_products_edit(self):
        """
        Tests the "api_products_edit" API method.
        Success conditions :
        - The API can't be accessed without being logged
        - Perfoms correctly ADD / EDIT  operations
        """
        with webapp.test_client() as app:
            # Tests access without being authenticated
            response = app.get('/api/products_edit/foo/bar')
            assert response.status_code == 401

            # Authenticate
            data = {'password': self.password_raw}
            response = app.post('/', data=data, follow_redirects=True)

            # Test : add
            barcode = '1000000000001'
            name = 'foobar'

            response = app.get('/api/products_edit/' + barcode + '/' + name)
            entry = models.Products().get_item(barcode)
            assert response.status_code == 200
            assert entry
            assert entry['barcode'] == barcode
            assert entry['name'] == name

            # Test : edit
            name = 'barfoo'
            response = app.get('/api/products_edit/' + barcode + '/' + name)
            entry = models.Products().get_item(barcode)
            assert response.status_code == 200
            assert entry
            assert entry['barcode'] == barcode
            assert entry['name'] == name
Exemplo n.º 2
0
    def test_api_products_list(self):
        """
        Tests the "api_products_list" API method.
        Success conditions :
        - The API can't be accessed without being logged : HTTP 401
        - Returns HTTP 200 when logged with the expected content as JSON
        """
        with webapp.test_client() as app:
            # Tests access without being authenticated
            response = app.get('/api/products_list')
            assert response.status_code == 401

            # Authenticate
            data = {'password': self.password_raw}
            response = app.post('/', data=data, follow_redirects=True)

            # Does the API returns the expected data ?
            # JSON "item" entry must contain the same thing as Products > get_list
            response = app.get('/api/products_list')

            expected_data = models.Products().get_list()
            given_data = str(response.data, encoding='utf-8')
            given_data = json.loads(given_data)
            given_data = given_data['items'].keys()

            assert response.status_code == 200
            assert set(expected_data) == set(given_data)
Exemplo n.º 3
0
    def test_login(self):
        """
        Tests the login process, with both valid and invalid input.
        Success conditions :
        - Valid : HTTP 200, session "is_logged" = True
        - Invalid : HTTP 200, session "is_logged" doesn't exist or is False
        """
        # Invalid
        with webapp.test_client() as app:
            data = {'password': '******'}
            response = app.post('/', data=data, follow_redirects=True)
            assert response.status_code == 200
            assert 'is_logged' not in session or not session['is_logged']

        # Valid
        with webapp.test_client() as app:
            data = {'password': self.password_raw}
            response = app.post('/', data=data, follow_redirects=True)
            assert response.status_code == 200
            assert session['is_logged']
Exemplo n.º 4
0
    def test_lang(self):
        """
        Tests access to the "lang" JSON content
        Success conditions :
        - Returns HTTP 200 with associated JSON data
        """
        with webapp.test_client() as app:
            response = app.get('/api/lang')

            expected_data = utils.Lang('en').__dict__
            given_data = str(response.data, encoding='utf-8')
            given_data = json.loads(given_data)
            given_data = given_data.keys()

            assert response.status_code == 200
            assert set(expected_data) == set(given_data)
Exemplo n.º 5
0
    def test_logout(self):
        """
        Tests the logout process.
        Success conditions :
        - HTTP 200, session "is_logged" doesn't exist anymore
        """
        with webapp.test_client() as app:
            # Log in
            data = {'password': self.password_raw}
            response = app.post('/', data=data, follow_redirects=True)
            assert response.status_code == 200
            assert session['is_logged']

            # Log out
            response = app.get('/logout', follow_redirects=True)
            assert response.status_code == 200
            assert 'is_logged' not in session
Exemplo n.º 6
0
    def test_products(self):
        """
        Tests access to the "products" page.
        Success conditions :
        - The page can't be accessed without being logged (returns HTTP 302)
        - Returns HTTP 200 when logged
        """
        with webapp.test_client() as app:
            # Tests access without being authenticated
            response = app.get('/products')
            assert response.status_code == 302

            # Tests access while being authenticated
            data = {'password': self.password_raw}
            response = app.post('/', data=data, follow_redirects=True)

            response = app.get('/products')
            assert response.status_code == 200
Exemplo n.º 7
0
    def test_api_products_delete(self):
        """
        Tests the "api_products_delete" API method.
        Success conditions :
        - The API can't be accessed without being logged
        - Perfoms correctly DELETE operations
        """
        with webapp.test_client() as app:
            # Tests access without being authenticated
            response = app.get('/api/products_edit/foo/bar')
            assert response.status_code == 401

            # Authenticate
            data = {'password': self.password_raw}
            response = app.post('/', data=data, follow_redirects=True)

            # Test : Delete
            response = app.get('/api/products_delete/' + self.default_barcode)
            assert response.status_code == 200
            assert not models.Products().get_item(self.default_barcode)
Exemplo n.º 8
0
    def test_api_groceries_edit(self):
        """
        Tests the "api_groceries_edit" API method.
        Success conditions :
        - The API can't be accessed without being logged : HTTP 401
        - Returns HTTP 200 when logged with the expected content as JSON
        - Perfoms correctly ADD / EDIT / DELETE operations
        """
        with webapp.test_client() as app:
            # Tests access without being authenticated
            response = app.get('/api/groceries_edit/' + self.default_barcode +
                               '/1')
            assert response.status_code == 401

            # Authenticate
            data = {'password': self.password_raw}
            response = app.post('/', data=data, follow_redirects=True)

            # Test : delete, valid input
            response = app.get('/api/groceries_edit/' + self.default_barcode +
                               '/0')
            assert response.status_code == 200
            assert not models.Groceries().get_item(self.default_barcode)

            # Test : add, valid input
            response = app.get('/api/groceries_edit/' + self.default_barcode +
                               '/2')
            entry = models.Groceries().get_item(self.default_barcode)
            assert response.status_code == 200
            assert entry
            assert entry['barcode'] == self.default_barcode
            assert entry['quantity'] == 2

            # Test : edit, valid input
            response = app.get('/api/groceries_edit/' + self.default_barcode +
                               '/4')
            entry = models.Groceries().get_item(self.default_barcode)
            assert response.status_code == 200
            assert entry
            assert entry['barcode'] == self.default_barcode
            assert entry['quantity'] == 4