예제 #1
0
    def test_create_favorite_with_invalid_category_fails(
            self, client, init_db):
        """
        Parameters:
            client(FlaskClient): fixture to get flask test client
            init_db(SQLAlchemy): fixture to initialize the test database
        """

        payload = {
            'title': fake.alphanumeric(10),
            'description': fake.alphanumeric(20),
            'metaData': {
                'color': 'black',
                'quantity': 20
            },
            'ranking': 2,
            'categoryId': 2121
        }

        response = client.post(f'{BASE_URL}/favorites',
                               data=json.dumps(payload),
                               content_type='application/json')

        data = json.loads(response.data.decode())

        assert response.status_code == 400
        assert data['status'] == 'error'
        assert data['message'] == 'An error occurred'
        assert data['errors']['categoryId'] == [
            ERROR_MESSAGES['NOT_FOUND_IDENTIFIER'].format('category')
        ]
예제 #2
0
    def test_create_favorite_with_incomplete_description_fails(
            self, client, init_db, category):
        """
        Parameters:
            client(FlaskClient): fixture to get flask test client
            init_db(SQLAlchemy): fixture to initialize the test database
            category (Category): Fixture to create a new category
        """

        payload = {
            'title': fake.alphanumeric(10),
            'description': fake.alphanumeric(4),  #Description is less than 10
            'ranking': 3,
            'metaData': {
                'color': 'black',
                'quantity': 20
            },
            'categoryId': category.id
        }

        response = client.post(f'{BASE_URL}/favorites',
                               data=json.dumps(payload),
                               content_type='application/json')

        data = json.loads(response.data.decode())

        assert response.status_code == 400
        assert data['status'] == 'error'
        assert data['message'] == 'An error occurred'
        assert data['errors']['description'][0] == ERROR_MESSAGES[
            'STRING_LENGTH'].format(10)
예제 #3
0
    def test_create_favorite_with_valid_data_succeeds(self, client, init_db,
                                                      category4):
        """
        Parameters:
            client(FlaskClient): fixture to get flask test client
            init_db(SQLAlchemy): fixture to initialize the test database
            category (Category): Fixture to create a new category
        """
        payload = {
            'title': fake.alphanumeric(10),
            'description': fake.alphanumeric(200),
            'metaData': {
                'color': 'red',
                'quantity': 100,
                'condition': 'good'
            },
            'ranking': 3,
            'categoryId': category4.id
        }

        response = client.post(f'{BASE_URL}/favorites',
                               data=json.dumps(payload),
                               content_type='application/json')

        data = json.loads(response.data.decode())

        assert response.status_code == 201
        assert data['status'] == 'success'
        assert data['message'] == SUCCESS_MESSAGES['CREATED'].format(
            'Favorite')
        assert data['data']['title'] == payload['title']
        assert data['data']['description'] == payload['description']
예제 #4
0
    def test_create_favorite_with_missing_ranking_fails(
            self, client, init_db, category):
        """
        Parameters:
            client(FlaskClient): fixture to get flask test client
            init_db(SQLAlchemy): fixture to initialize the test database
            category (Category): Fixture to create a new category
        """

        payload = {
            'title': fake.alphanumeric(10),
            'description': fake.alphanumeric(20),
            'metaData': {
                'color': 'black',
                'quantity': 20
            },
            'categoryId': category.id
        }

        response = client.post(f'{BASE_URL}/favorites',
                               data=json.dumps(payload),
                               content_type='application/json')

        data = json.loads(response.data.decode())
        assert response.status_code == 400
        assert data['status'] == 'error'
        assert data['message'] == 'An error occurred'
        assert data['errors']['ranking'] == [
            'Missing data for required field.'
        ]
예제 #5
0
def favorite(app, category3):
    params = {
        'title' : fake.alphanumeric(15),
        'description' : fake.alphanumeric(200),
        'ranking' : 1,
        'meta_data' : {
            'color' : 'red',
            'quantity' : 2,
            'date_purchased' : '2019-02-05',
            'condition' : 'bad'
        },
        'category_id' : category3.id
    }
    favorite = Favorite(**params)
    return favorite.save()
예제 #6
0
    def test_create_risk_with_valid_data_succeeds(
            self, client, init_db, user_one, new_risk_type_with_attribute):
        """
        Parameters:
            client(FlaskClient): fixture to get flask test client
            init_db(SQLAlchemy): fixture to initialize the test database
            user_one (User): Fixture to create a new user
            new_risk_type_with_attribute (RiskType): Fixture to create a new risk type
        """
        payload = {
            'data': {
                'model': fake.alphanumeric(10),
            },
            'riskTypeId': new_risk_type_with_attribute.id
        }

        response = client.post(
            f'{BASE_URL}/risks',
            data=json.dumps(payload),
            headers=dict(Authorization=f'Bearer {user_one.token}'),
            content_type='application/json')

        data = json.loads(response.data.decode())
        assert response.status_code == 201
        assert data['status'] == 'success'
        assert data['message'] == SUCCESS_MESSAGES['CREATED'].format('Risk')
예제 #7
0
def categories(app):
    categories = []
    for each in range(3):
        params = {'name': fake.alphanumeric()}
        category = Category(**params)
        categories.append(category.save())
    return categories
예제 #8
0
    def test_user_logout(self, init_db):
        """

        Args:
            init_db(SQLAlchemy): fixture to initialize the test database
        """
        status = User.logout(fake.alphanumeric())
        assert status == True
예제 #9
0
def category_with_favorites(app, category2):
    favorites = []
    for each in range(3):
        params = {
            'title' : fake.alphanumeric(15),
            'description' : fake.alphanumeric(200),
            'ranking' : 1,
            'meta_data' : {
                'color' : 'red',
                'quantity' : 2,
                'date_purchased' : '2019-02-05',
                'condition' : 'bad'
            },
            'category_id' : category2.id
        }
        favorite = Favorite(**params)
        favorites.append(favorite.save())
    return category2
예제 #10
0
 def test_save(self, init_db):
     """Test for creating a new suspended_token
     
         Args:
             init_db(SQLAlchemy): fixture to initialize the test database
     """
     params = {'token': fake.alphanumeric(50)}
     suspended_token = SuspendedToken(**params)
     assert suspended_token == suspended_token.save()
예제 #11
0
 def test_update(self, init_db, category):
     """Test for update method
     
         Args:
             init_db(SQLAlchemy): fixture to initialize the test database
             category (Category): Fixture to create a new category
     """
     category_name = fake.alphanumeric()
     category.update(name=category_name)
     assert category.name == category_name
예제 #12
0
 def test_save(self, init_db):
     """Test for creating a new company
     
         Args:
             init_db(SQLAlchemy): fixture to initialize the test database
     """
     params = {
         'name': fake.alphanumeric(15)
     }
     category = Category(**params)
     assert category == category.save()
예제 #13
0
    def test_save(self, init_db, category1):
        """Test creating new favorite

        Args:
            init_db (SQLAlchemy): fixture to initialize the test database
            category (Category): Fixture to create a new category
        """
        params = {
            'title' : fake.alphanumeric(15),
            'description' : fake.alphanumeric(200),
            'ranking' : 1,
            'meta_data' : {
                'color' : 'red',
                'quantity' : 2,
                'date_purchased' : '2019-02-05',
                'condition' : 'bad'
            },
            'category_id' : category1.id
        }

        favorite = Favorite(**params)
        assert favorite == favorite.save()
예제 #14
0
 def test_update(self, init_db, favorite):
     """Test for update method
     
         Args:
             init_db(SQLAlchemy): fixture to initialize the test database
             favorite (Favorite): Fixture to create a new favorite
     """
     params = {
         "title": "Category",
         "description": fake.alphanumeric(100),
     }
     favorite.update(**params)
     assert favorite.title == params['title']
     assert favorite.description == params['description']
예제 #15
0
    def test_create_category_with_valid_data_succeeds(self, client, init_db):
        """
        Parameters:
            client(FlaskClient): fixture to get flask test client
            init_db(SQLAlchemy): fixture to initialize the test database
        """
        payload = {'name': fake.alphanumeric(14)}

        response = client.post(f'{BASE_URL}/categories',
                               data=json.dumps(payload),
                               content_type='application/json')

        response_json = json.loads(response.data.decode())
        assert response.status_code == 201
        assert response_json['status'] == 'success'
        assert response_json['message'] == SUCCESS_MESSAGES['CREATED'].format(
            'Category')

        data = response_json['data']
        assert data['name'] == payload['name']
예제 #16
0
    def test_update_a_category_with_valid_data_succeeds(
            self, client, init_db, category):
        """
        Args:
            client(FlaskClient): fixture to get flask test client
            init_db(SQLAlchemy): fixture to initialize the test database
            category (Category): Fixture to create a new category
        """
        params = {
            'name': fake.alphanumeric(20),
        }

        response = client.put(f'{BASE_URL}/categories/{category.id}',
                              data=json.dumps(params),
                              content_type='application/json')

        response_json = json.loads(response.data.decode())
        assert response.status_code == 200
        assert response_json['status'] == 'success'
        assert response_json['message'] == SUCCESS_MESSAGES['UPDATED'].format(
            'Category')

        data = response_json['data']
        assert data['name'] == params['name']
예제 #17
0
def new_suspended_token(app):
    params = {
        'token': fake.alphanumeric(50),
    }
    suspended_token = SuspendedToken(**params)
    return suspended_token.save()
예제 #18
0
def category(app):
    params = {
        'name': fake.alphanumeric(),
    }
    category = Category(**params)
    return category.save()
예제 #19
0
from api.views.utils import get_auth_token
from tests.base import fake

token = fake.alphanumeric(30)


class RequestMock:
    @property
    def headers(self):
        return dict(Authorization=f'Bearer {token}')


request = RequestMock()


class TestUtilities:
    def test_get_auth_token(self):
        """ tests that the method retrieves token from the request headers"""
        auth_token = get_auth_token(request)
        assert auth_token == token