示例#1
0
    def create_attributes(self, count: int) -> None:
        """
        Create Attributes for SubThemes
        :param count: Number of Attributes per SubTheme
        """
        number_attributes = len(self.sub_themes) * count

        try:
            index = 0
            sub_theme = self.sub_themes[index]
            for num in range(0, number_attributes):

                unit = self.create_unit(num)
                attr = Attributes(
                    "attribute_id_{}".format(num),
                    "attribute_name_{}".format(num),
                    "b3_heat_value_bffc4e56_20e2_41a5_84d8_de725a3f875b",
                    sub_theme.id, unit.id, "_test_description_{}".format(num),
                    "1")
                attr.save()
                attr.commit()
                self.attributes.append(attr)

                if num % count:
                    index += 1
                    if index >= len(self.sub_themes):
                        return
                    sub_theme = self.sub_themes[index]

        except Exception as exp:
            logger.error(exp)
            self.fail()
示例#2
0
    def test_return_attribute_of_subtheme(self):
        """
        Adding a theme and subtheme to the database and then testing to see if it's data is
        retrieved correctly
        """

        theme = Theme("Test_Theme")
        theme.save()
        theme.commit()

        sub_theme = SubTheme(theme.id, "Test_Sub_Theme")
        sub_theme.save()
        sub_theme.commit()

        attributes = Attributes("1234567890-123456789-123456789",
                                "_test_attribute_", "_table_name_",
                                sub_theme.id, 1)
        attributes.save()
        attributes.commit()

        response = self.testing_client.get('/data',
                                           data=dict(subtheme=theme.id))

        self.assertEqual(theme.json(), response.get_json())

        attributes.delete()
        attributes.commit()

        sub_theme.delete()
        sub_theme.commit()

        theme.delete()
        theme.commit()
class TestAttrAlias(TestCase):
    """
    Unittest Attribute Alias, Test Endpoints and Database persistence.
    """

    def setUp(self):
        """
        Setup a FlaskClient, AppContext, Admin user, Authorization header for requests to
        the Flask Client and a dummy Theme
        """
        self.client, self.app_context = self.create_test_client()
        self.user = self.create_admin_user()
        self.auth_header = self.get_auth_header()
        self.unit = Unit("_test_unit_type_", "A test unit")
        self.unit.save()
        self.unit.commit()
        self.attribute = Attributes.get_by_name("_test_attribute_")
        self.theme = self.create_dummy_theme()
        self.subtheme = self.create_dummy_subtheme()
        if not self.attribute:
            self.attribute = Attributes("1234567890-123456789-123456789", "_test_attribute_", "_table_name_",
                                        self.subtheme.id, self.unit.id)
            self.attribute.save()
            self.attribute.commit()

        self.clean_up = [self.user, self.attribute, self.theme, self.subtheme, self.theme, self.unit]

    def create_test_client(self) -> (FlaskClient, AppContext):
        """
        Create FlaskClient and AppContext
        :return:    FlaskClient and AppContext
        """
        test_app = create_app(DATABASE_NAME='test_analysis', TESTING=True)
        testing_client = test_app.test_client()
        test_app_context = test_app.app_context()
        test_app_context.push()
        return testing_client, test_app_context

    def create_dummy_theme(self) -> Theme:
        """
        Create a Theme
        :return: a Theme
        """
        theme = Theme.get_by_name("_test_add_theme_")
        if not theme:
            theme = Theme("_test_add_theme_")
            theme.save()
            theme.commit()
            return theme
        return theme

    def create_dummy_subtheme(self) -> SubTheme:
        """
        Create SubTheme
        :return: SubTheme
        """
        subtheme = SubTheme.get_by_name('_TEST_SUB_THEME_')
        if not subtheme:
            subtheme = SubTheme(self.theme.id, '_TEST_SUB_THEME_')
            subtheme.save()
            subtheme.commit()
            subtheme = SubTheme.get_by_name('_TEST_SUB_THEME_')
        return subtheme

    def create_admin_user(self) -> Users:
        """
        Create Admin user
        :return: an admin user
        """
        password_hash = bcrypt.hashpw("wfnbqk".encode("utf-8"), bcrypt.gensalt())
        user = Users.find_by_email("*****@*****.**")
        if not user:
            user = Users("Admin", "*****@*****.**", password_hash.decode("utf8"), True, True)
            try:
                user.save()
                user.commit()
            except Exception as e:
                pass
        return user

    def get_auth_header(self) -> {str: str}:
        """
        Create an Authorization header
        :return: An authorization header
        """
        response_login = self.client.post('/login', data=dict(email=self.user.email, password="******", remember=True),
                                          follow_redirects=True)
        response_login_json = response_login.get_json()
        return {'Authorization': 'Bearer {}'.format(response_login_json["access_token"])}

    def get_dummy_alias(self) -> db.Model:
        """
        Create dummy AttrAlias
        :return: A AttrAlias
        """
        alias = AttrAlias.get_by(name="_custom_name")
        if not alias:
            alias = AttrAlias(self.attribute.id, self.user.id, name="_custom_name", table_name="_table_name_",
                              description="a custon description")
            alias.save()
            alias.commit()
            self.clean_up.append(alias)
        return alias

    def test_create_alias(self) -> None:
        """
        Create AttrAlias database entry using database session
        """
        alias = AttrAlias(self.attribute.id, self.user.id, name="_custom_name", table_name="_table_name_",
                          description="a custon description")
        self.assertTrue(alias, msg="test_create_alias(): Failed to create Alias")
        if alias:
            self.clean_up.append(alias)

    def test_create_alias(self) -> None:
        """ Create AttrAlias using endpoint and check client response for https status code 200"""
        json_payload = {"user_id": self.user.id, "attribute_id": self.attribute.id, "name": "A_Custom_Name_12890",
                        "table_name": "_A_TaBlE_NaMe_"}
        response = self.client.post('/admin/attributes/alias', json=json_payload, headers=self.auth_header)
        if response.status_code == HTTPStatus.OK:
            alias = AttrAlias.get_by(user_id=self.user.id, name="A_Custom_Name_12890")
            if alias:
                self.clean_up.append(alias)
        self.assertEqual(response.status_code, HTTPStatus.OK)

    def test_update_alias(self) -> None:
        """ Update AttrAlias using endpoint, Check client response for https status code 200 and update table_name"""
        alias = self.get_dummy_alias()
        json_payload = {"user_id": self.user.id, "attribute_id": self.attribute.id, "name": "A_Custom_Name_12890",
                        "table_name": "_UpDaTeD_TaBlE_NaMe_"}
        response = self.client.post('/admin/attributes/alias', json=json_payload, headers=self.auth_header)
        if response.status_code == HTTPStatus.OK:
            alias = AttrAlias.get_by(user_id=self.user.id, name="A_Custom_Name_12890")
            if alias:
                self.clean_up.append(alias)
        self.assertEqual(response.status_code, HTTPStatus.OK)
        self.assertEqual(alias.table_name, "_UpDaTeD_TaBlE_NaMe_")

    def test_delete_alias(self) -> None:
        """Delete AttrAlias using endpoint and check client response for https status code 204"""
        alias = self.get_dummy_alias()
        self.assertTrue(alias)
        json_payload = {"user_id": self.user.id, "attribute_id": self.attribute.id}
        response = self.client.post('/admin/attributes/delete_alias', json=json_payload, headers=self.auth_header)
        self.assertEqual(response.status_code, HTTPStatus.NO_CONTENT)

    def delete(self, module: db.Model) -> None:
        module.delete()
        module.commit()

    def tearDown(self) -> None:
        """
        Clean up dependencies
        """
        self.subtheme.delete()
        self.subtheme.commit()

        self.theme.delete()
        self.theme.commit()
        db.engine.execute("DELETE FROM attributes WHERE id = 'A_TEST_ATTRIBUTE_ID_'")
        # self.delete(self.attribute)

        self.unit.delete()
        self.unit.commit()

        self.user.delete()
        self.user.commit()

        map(self.delete,self.clean_up)
        self.app_context.pop()
示例#4
0
class TestGetAttributes(TestCase):
    """ Test get_attribute endpoint """
    def setUp(self) -> None:
        """
        Setup FlaskClient for tests, create an admin user and create the authorization header for requests to
        the FlaskClient
        """
        self.clean_up = []
        self.client, self.app_context = self.create_test_client()
        self.user = self.create_admin_user()
        self.theme = self.create_dummy_theme()
        self.subtheme = self.create_dummy_subtheme()
        self.unit = self.get_dummy_unit()
        self.attribute = Attributes("A_TEST_ATTRIBUTE_ID_",
                                    "_TEST_ATTR_NAME_",
                                    "_A_TABLE_NAME",
                                    self.subtheme.id,
                                    self.unit.id,
                                    description="_A_CUSTOM_DESCRIPTION_")
        self.attribute.save()
        self.attribute.commit()
        self.clean_up.append(self.attribute)

    def create_test_client(self) -> (FlaskClient, AppContext):
        """
        Create FlaskClient and AppContext
        :return:    FlaskClient and AppContext
        """
        test_app = create_app(DATABASE_NAME='test_analysis', TESTING=True)
        testing_client = test_app.test_client()
        test_app_context = test_app.app_context()
        test_app_context.push()
        return testing_client, test_app_context

    def create_dummy_theme(self) -> Theme:
        """
        Create a Theme
        :return: a Theme
        """
        theme = Theme.get_by_name("_test_add_theme_")
        if not theme:
            theme = Theme("_test_add_theme_")
            theme.save()
            theme.commit()
            self.clean_up.append(theme)
            return theme
        return theme

    def create_dummy_subtheme(self) -> SubTheme:
        """
        Create SubTheme
        :return: SubTheme
        """
        subtheme = SubTheme.get_by_name('_TEST_SUB_THEME_')
        if not subtheme:
            subtheme = SubTheme(self.theme.id, '_TEST_SUB_THEME_')
            subtheme.save()
            subtheme.commit()
            self.clean_up.append(subtheme)
            subtheme = SubTheme.get_by_name('_TEST_SUB_THEME_')
        return subtheme

    def get_dummy_unit(self):
        unit = Unit.get_by_symbol("_A_UNIT_FOR_TESTING")
        if not unit:
            unit = Unit("_A_UNIT_FOR_TESTING", "_A_TEST_UNIT_DESCRIPTION_")
            unit.save()
            unit.commit()
        if unit not in self.clean_up:
            self.clean_up.append(unit)

        return unit

    def create_admin_user(self) -> Users:
        """
        Create Admin user
        :return: an admin user
        """
        password_hash = bcrypt.hashpw("wfnbqk".encode("utf-8"),
                                      bcrypt.gensalt())
        user = Users.find_by_email("*****@*****.**")
        if not user:
            user = Users("Admin", "*****@*****.**",
                         password_hash.decode("utf8"), True, True)
            try:
                user.save()
                user.commit()
            except Exception as e:
                pass
        self.clean_up.append(user)
        return user

    def get_auth_header(self) -> {str: str}:
        """
        Create an Authorization header
        :return: An authorization header
        """
        response_login = self.client.post('/login',
                                          data=dict(email=self.user.email,
                                                    password="******",
                                                    remember=True),
                                          follow_redirects=True)
        response_login_json = response_login.get_json()
        return {
            'Authorization':
            'Bearer {}'.format(response_login_json["access_token"])
        }

    def test_get_all_attributes(self) -> None:
        """ Fetch all Attributes using endpoint"""
        response = self.client.get('/admin/attributes/get_attributes')
        self.assertEqual(response.status_code, HTTPStatus.OK)

    def test_get_attribute_by_id(self) -> None:
        """ Fetch Attribute by id using endpoint"""
        query_string_data = {"attribute_id": self.attribute.id}
        response = self.client.get('/admin/attributes/get_attributes',
                                   data=query_string_data)
        self.assertEqual(response.status_code, HTTPStatus.OK)
        json_response = response.get_json()
        self.assertEqual(json_response[0]["id"],
                         self.attribute.id,
                         msg="Attribute id does not match")
        self.assertEqual(json_response[0]["name"],
                         self.attribute.name,
                         msg="Attribute name does not match")

    def delete(self, module: db.Model) -> None:
        """
        Delete db>Model instances from the database
        :param module: Model to be deleted
        :type module: db>Model
        """
        module.delete()
        module.commit()

    def tearDown(self) -> None:
        """
        Clean up dependencies
        """
        self.subtheme.delete()
        self.subtheme.commit()

        self.theme.delete()
        self.theme.commit()
        db.engine.execute(
            "DELETE FROM attributes WHERE id = 'A_TEST_ATTRIBUTE_ID_'")
        # self.delete(self.attribute)

        self.unit.delete()
        self.unit.commit()

        self.user.delete()
        self.user.commit()

        map(self.delete, self.clean_up)
        self.app_context.pop()