Esempio n. 1
0
    def test_check_token_exists(self, session):
        """
        Test for check_token_exists
        """
        token = None
        result = check_token_exists(token)
        assert result == None

        token = "fkdjasghgopgasdf"
        result = check_token_exists(token)
        assert result == None

        user = next((x for x in self.user_list if x.name == "Test"), None)
        old_token = str(user.id).zfill(10) + str(uuid.uuid4())
        #Check if token is unique
        while (UserToken.query.filter_by(token=old_token).count() != 0):
            old_token = str(user.id).zfill(10) + str(uuid.uuid4())

        old_token = UserToken(user_id=user.id, token=old_token)
        old_token.update = datetime.utcnow() - timedelta(hours=4)
        old_token.created = datetime.utcnow() - timedelta(hours=48)
        session.add(old_token)
        session.commit()

        result = check_token_exists(token)
        assert result == None

        result = check_token_exists(old_token.token)
        assert result == old_token
Esempio n. 2
0
    def test_authenticate(self, session):
        """
        Authenticate test
        """
        result = authenticate()
        assert result == None

        user = next((x for x in self.user_list if x.name == "Test"), None)

        old_token = str(user.id).zfill(10) + str(uuid.uuid4())
        #Check if token is unique
        while (UserToken.query.filter_by(token=old_token).count() != 0):
            old_token = str(user.id).zfill(10) + str(uuid.uuid4())

        old_token = UserToken(user_id=user.id, token=old_token)
        old_token.update = datetime.utcnow() - timedelta(hours=4)
        old_token.created = datetime.utcnow() - timedelta(hours=48)
        session.add(old_token)
        session.commit()

        result = authenticate(old_token.token)
        assert result == None

        result = authenticate(None, True, old_token.user_id)
        assert result != None
        assert result != old_token.token
        token_from_db = session.query(UserToken).filter_by(
            user_id=user.id).order_by(UserToken.id.desc()).first()
        assert result.token == token_from_db.token
Esempio n. 3
0
    def test_is_user_authorised(self, session, client):
        """
        Test user authorised method
        """
        token = "InvalidToken"
        url = url_for('user.is_user_authorised')

        data = {'token': token}

        result = client.post(url, json=data)
        result_json = result.get_json()
        assert result.status == "403 FORBIDDEN"
        assert result_json == "Invalid token."

        token = self.token_list[1]

        data = {'token': token}

        result = client.post(url, json=data)
        result_json = result.get_json()
        assert result.status == "200 OK"
        assert 'verified' in result_json
        assert result_json['verified'] == "false"

        user_id = self.user_list[0].id

        ttoken = str(user_id).zfill(10) + str(uuid.uuid4())
        #Check if token is unique
        while (UserToken.query.filter_by(token=ttoken).count() != 0):
            ttoken = str(user_id).zfill(10) + str(uuid.uuid4())

        ttoken = UserToken(user_id=user_id, token=ttoken)
        ttoken.update = datetime.utcnow() - timedelta(hours=4)
        ttoken.created = datetime.utcnow() - timedelta(hours=48)
        session.add(ttoken)
        self.token_list.append(ttoken)
        session.commit()

        data['token'] = ttoken.token

        result = client.post(url, json=data)
        result_json = result.get_json()
        assert result.status == "403 FORBIDDEN"
        assert result_json == "Invalid token."

        token = self.token_list[0]

        data = {'token': token}

        result = client.post(url, json=data)
        result_json = result.get_json()
        assert result.status == "200 OK"
        assert 'verified' in result_json
        assert result_json['verified'] == "true"
Esempio n. 4
0
 def test_get_token_all(self, session):
     """Test method get_token_all in UserToken"""
     assert len(UserToken.get_token_all()) == 1
     token = UserToken(self.userList[0].id, 'token_placeholder2')
     session.add(token)
     session.commit()
     self.tokenList.append(token)
     assert len(UserToken.get_token_all()) == 2
     session.query(UserToken).delete()
     session.commit()
     self.tokenList[:] = []
     assert len(UserToken.get_token_all()) == 0
Esempio n. 5
0
    def test_get_token_by_id(self):
        """Test method get_token_by_id in UserToken"""
        #Test something that really shouldn't be there
        with pytest.raises(DbException) as err:
            UserToken.get_token_by_id(0)
        assert str(err.value) == "(404, 'UserToken not found.')"

        token = UserToken.get_token_by_id(self.tokenList[0].id)
        assert token.token == self.tokenList[0].token
        assert token.id == self.tokenList[0].id

        assert UserToken.get_token_by_id(0, False) == None
Esempio n. 6
0
    def setup(self, session):
        """
        SETUP funcion for TestUser class, this function is executed for each function in this TestClass
        """
        user_types = []
        user_types.append(UserType("ADMIN", 2))
        user_types.append(UserType("GUEST", 0))
        user_types.append(UserType("USER", 1))

        user_type_db = UserType.query.all()

        for user in user_type_db:
            for seedUser in user_types:
                if (user.name == seedUser.name):
                    user_types.remove(seedUser)

        for user_type in user_types:
            self.user_type_list.append(user_type)
            session.add(user_type)
            session.commit()

        user = User("Admin", "*****@*****.**", "SuperSecret", None, None,
                    True)
        user.user_type_id = UserType.get_type_by_name('ADMIN').id
        user.activated = True
        session.add(user)
        session.commit()
        ttoken = str(user.id).zfill(10) + str(uuid.uuid4())
        #Check if token is unique
        while (UserToken.query.filter_by(token=ttoken).count() != 0):
            ttoken = str(user.id).zfill(10) + str(uuid.uuid4())

        ttoken = UserToken(user_id=user.id, token=ttoken)
        ttoken.update = datetime.utcnow()
        ttoken.created = datetime.utcnow()
        session.add(ttoken)
        session.commit()
        self.token_list.append(ttoken.token)
        self.user_list.append(user.id)
        user = User("Test", "*****@*****.**", "SuperSecret", None, None, True)
        session.add(user)
        session.commit()
        ttoken = str(user.id).zfill(10) + str(uuid.uuid4())
        #Check if token is unique
        while (UserToken.query.filter_by(token=ttoken).count() != 0):
            ttoken = str(user.id).zfill(10) + str(uuid.uuid4())

        ttoken = UserToken(user_id=user.id, token=ttoken)
        ttoken.update = datetime.utcnow()
        ttoken.created = datetime.utcnow()
        session.add(ttoken)
        session.commit()
        self.token_list.append(ttoken.token)
        self.user_list.append(user.id)

        yield
        session.query(User).delete()
        session.query(UserType).delete()
        session.query(UserToken).delete()
        session.commit()
Esempio n. 7
0
    def test_stats(self, session, client):
        """
        Tests stats in miscapi.py
        """
        #create valid token
        user_id = self.user_list[0]
        token = str(user_id).zfill(10) + str(uuid.uuid4())
        #Check if token is unique
        while (UserToken.query.filter_by(token=token).count() != 0) :
            token = str(user_id).zfill(10) + str(uuid.uuid4())

        token = UserToken(user_id=user_id,token=token)
        token.update = datetime.utcnow()
        token.created = datetime.utcnow()
        session.add(token)
        session.commit()

        result = client.get(url_for('miscapi.stats') + '?token=' + token.token)
        result_data = result.get_json()
        assert result_data is not None
        assert 'user_count' in result_data
        assert 'module_count' in result_data
        assert 'project_count' in result_data
        assert 'package_count' in result_data

        result = client.get(url_for('miscapi.stats') + '?token=bladfjoagsag')
        result_data = result.get_json()
        assert result_data is not None
        assert result.status == "403 FORBIDDEN"
        assert result_data == "Invalid token."

        result = client.get(url_for('miscapi.stats'))
        result_data = result.get_json()
        assert result_data is not None
        assert result.status == "403 FORBIDDEN"
        assert result_data == "Invalid token."
Esempio n. 8
0
    def setup(self, session):
        """
        SETUP funcion for TestUser class, this function is executed for each function in this TestClass
        """
        user_types = []
        user_types.append(UserType("ADMIN", 2))
        user_types.append(UserType("GUEST", 0))
        user_types.append(UserType("USER", 1))

        user_type_db = UserType.query.all()

        for user in user_type_db:
            for seedUser in user_types:
                if (user.name == seedUser.name):
                    user_types.remove(seedUser)

        for user_type in user_types:
            session.add(user_type)
            session.commit()

        user = User("Admin", "*****@*****.**", "SuperSecret", None, None,
                    True)
        user.user_type_id = UserType.get_type_by_name('ADMIN').id
        user.activated = True
        session.add(user)
        session.commit()
        self.userList.append(user)
        token = UserToken(self.userList[0].id, 'token_placeholder')
        session.add(token)
        session.commit()
        self.tokenList.append(token)
        yield
        session.query(UserToken).delete()
        session.query(User).delete()
        session.query(UserType).delete()
        session.commit()
        self.tokenList[:] = []
        self.userList[:] = []
Esempio n. 9
0
    def test_get_token_all_by_user_id(self, session):
        """Test method get_token_all_by_user_id in UserToken"""
        assert len(UserToken.get_token_all_by_user_id(
            self.userList[0].id)) == 1
        token = UserToken(self.userList[0].id, 'token_placeholder2')
        session.add(token)
        session.commit()
        self.tokenList.append(token)
        assert len(UserToken.get_token_all_by_user_id(
            self.userList[0].id)) == 2
        session.query(UserToken).delete()
        session.commit()
        self.tokenList[:] = []
        assert len(
            UserToken.get_token_all_by_user_id(self.userList[0].id,
                                               False)) == 0

        #Test something that really shouldn't be there
        with pytest.raises(DbException) as err:
            UserToken.get_token_all_by_user_id(0)
        assert str(err.value) == "(404, 'No userToken found.')"