Exemplo n.º 1
0
class TestWeather():

    reset_database()

    @patch.object(PublicGetCurrentWeather, 'get')
    def test_get_weather_info_mock(self, mock_get):

        token = create_token_buyer()

        response = {
            "city": "Bandung",
            "organization": "AS9657 Melsa-i-net AS",
            "timezone": "Asia/Jakarta",
            "current_weather": {
                "date": "2019-09-04:07",
                "temp": 27.6
            }
        }

        mock_get.return_value = response

        assert PublicGetCurrentWeather.get(
            '/weather?ip=202.138.233.162',
            headers={'Authorization': 'Bearer ' + token}) == response

    def test_get_weather_info(self, client):
        token = create_token_buyer()
        res = client.get('/weather?ip=202.138.233.162',
                         headers={'Authorization': 'Bearer ' + token},
                         content_type='application/json')

        res_json = json.loads(res.data)
        assert res.status_code == 200
Exemplo n.º 2
0
class TestThirdPartyCrud():
    """class to test third party api"""

    reset_database()

    @patch.object(RecommendationPlaceToEat, 'get')
    def testGetZomatoMock(self, mock_get):
        """class to test third party api zomato with mock"""
        response = {
            "restaurants":
            "WAKI Japanese BBQ Dining",
            "address":
            "Lantai 1, Jl. Tanjung Karang No. 5, Thamrin, Jakarta",
            "photo":
            "https://b.zmtcdn.com/data/reviews_photos/621/2623a19855d87924b3e4271473d8c621_1566921104.jpg?fit=around%7C200%3A200&crop=200%3A200%3B%2A%2C%2A"
        }

        mock_get.return_value = response

        assert RecommendationPlaceToEat.get(
            'https://developers.zomato.com/api/v2.1/search?b875502a178dcc62abd9f3437d92fbe5'
        ) == response

    @patch.object(RecommendationPlaceToVacation, 'get')
    def testGetVacationMock(self, mock_get):
        """class to test third party api vacation with mock"""

        response = {
            "place":
            "Candi Borobudur",
            "place_location":
            "Magelang",
            "photo":
            "https://b.zmtcdn.com/data/reviews_photos/621/2623a19855d87924b3e4271473d8c621_1566921104.jpg?fit=around%7C200%3A200&crop=200%3A200%3B%2A%2C%2A"
        }

        mock_get.return_value = response

        assert RecommendationPlaceToVacation.get(
            'https://opentripmap-places-v1.p.rapidapi.com/en/places/bbox?lon_min=107&lon_max=113&lat_min=-7&lat_max=-2'
        ) == response

    @patch.object(RecommendationPlaceToHike, 'get')
    def testGetHikingMock(self, mock_get):
        """class to test third party api hiking with mock"""

        response = {
            "place":
            "Gunung Salak",
            "place_location":
            "Bogor",
            "photo":
            "https://b.zmtcdn.com/data/reviews_photos/621/2623a19855d87924b3e4271473d8c621_1566921104.jpg?fit=around%7C200%3A200&crop=200%3A200%3B%2A%2C%2A"
        }

        mock_get.return_value = response

        assert RecommendationPlaceToHike.get(
            'https://opentripmap-places-v1.p.rapidapi.com/en/places/bbox?lon_min=107&lon_max=113&lat_min=-7&lat_max=-2'
        ) == response
Exemplo n.º 3
0
class TestUser():
    reset_database()

    def test_user_is_exsist(self):

        username = "******"

        assert User.is_exists(username) == True
Exemplo n.º 4
0
class TestCategoriesCrud():
    """class to test dates resource"""

    reset_database()

    def test_categories_post_valid(self, client):
        token = create_token()
        data = {"preference": "kuntuk", "category": "makan"}
        res = client.post('/api/category',
                          data=json.dumps(data),
                          headers={'Authorization': 'Bearer ' + token},
                          content_type='application/json')
        res_json = json.loads(res.data)
        if res.status_code != 200:
            raise ValueError(
                'The res.status_code must be 200, please check your code')

    def test_categories_post_invalid_no_token(self, client):
        """test invalid post without token"""

        data = {"preference": "pedas", "category": "ohyeah"}
        res = client.post('/api/categorys',
                          data=json.dumps(data),
                          content_type='application/json')

        if res.status_code != 404:
            raise ValueError(
                'The res.status_code must be 404, please check your code')

    def test_categories_get_valid(self, client):
        token = create_token()
        data = {"category": "makan"}
        res = client.get('/api/category',
                         query_string=data,
                         headers={'Authorization': 'Bearer ' + token},
                         content_type='application/json')
        res_json = json.loads(res.data)
        if res.status_code != 200:
            raise ValueError(
                'The res.status_code must be 200, please check your code')

    def test_categories_get_invalid_no_token(self, client):
        """test invalid post without token"""

        data = {"category": "makan"}
        res = client.get('/api/category',
                         query_string=data,
                         content_type='application/json')
        res_json = json.loads(res.data)
        if res.status_code != 401:
            raise ValueError(
                'The res.status_code must be 401, please check your code')
class TestClient():

    reset_database()

    def test_is_client_key_already_exist(self):

        client_key = "seller_2"

        assert Clients.is_exists(client_key) == True

    def test_is_client_key_doesnt_exist(self):

        client_key = "seller_9"

        assert Clients.is_exists(client_key) == False
Exemplo n.º 6
0
class TestUnit():

    reset_database()

    def test_is_exist_function(self):
        assert UserResource.is_exist(self, 'admin') == True
        assert UserResource.is_exist(self, 'tidak_ada') == False

    @patch.object(CurrentWeather, 'get')
    def test_get_temp_function(self, mock_get):
        temp = {'temp': 22.5}
        mock_get.return_value = temp
        assert CurrentWeather.get() == temp

    # def test_get_temp_asli(self):
    # res = client.get('/bacaan')
    # assert res.status_code == 200
Exemplo n.º 7
0
class TestUser():

    reset_database()

    def test_is_user_already_exist(self):

        datas = [
            {
                "username": "******",
                "password": "******",
                "phonenumber": "094653438",
                "address": "Malang"
            },
            {
                "username": "******",
                "password": "******",
                "phonenumber": "097563782",
                "address": "Jakarta"
            }
        ]

        # insert data
        for data in datas:
            user = UserModel(data['username'], data['password'], data['phonenumber'], data['address'])
            db.session.add(user)
            db.session.commit()

        username = "******"

        assert UserModel.is_exists(username) == True


    @patch.object(UserModel, 'is_exists')
    def test_if_get_last_balance_is_none(self, mock_is_exists):

        username = "******"
        mock_is_exists.return_value = True

        assert UserModel.is_exists(username) == True
Exemplo n.º 8
0
class TestUsersCrud():
    """Test all request in users resource"""
    '''reset all database before testing'''
    reset_database()

    def test_users_get(self, client):
        token = create_token()
        res = client.get('/api/users', content_type='application/json')

        if res.status_code != 200:
            raise ValueError(
                'The res.status_code must be 200, please check your code!')

    def test_users_put(self, client):
        data = {
            'username': '******',
            'email': '*****@*****.**',
            'password': '******',
            'gender': False,
            'fullname': 'Agatha Ranpa',
            'address': 'blitar-jombang',
            'phone': '0822222137'
        }

        res = client.put('/api/users/1',
                         data=json.dumps(data),
                         content_type='application/json')

        if res.status_code != 200:
            raise ValueError(
                'The res.status_code must be 200, please check your code')

    def test_users_invalid_put(self, client):
        """ test invalid PUT with bad request 404 """

        data = {
            'username': '******',
            'email': '*****@*****.**',
            'password': '******',
            'gender': False,
            'fullname': 'Agatha',
            'address': 'blitar-jombang',
            'phone': '0822222137'
        }

        res = client.put('/api/users/7',
                         data=json.dumps(data),
                         content_type='application/json')

        if res.status_code != 404:
            raise ValueError(
                'The res.status_code must be 401, please check your code')

    def test_users_login(self, client):
        data = {
            'username': '******',
            'password': '******',
        }

        res = client.post('/api/users/login',
                          data=json.dumps(data),
                          content_type='application/json')

        if res.status_code != 200:
            raise ValueError(
                'The res.status_code must be 200, please check your code')

    def test_users_invalid_login_credential(self, client):
        """ test invalid POST with bad request 401 """

        data = {
            'username': '******',
            'password': '******',
        }

        res = client.post('/api/users/login',
                          data=json.dumps(data),
                          content_type='application/json')

        if res.status_code != 401:
            raise ValueError(
                'The res.status_code must be 401, please check your code')

    def test_users_invalid_login(self, client):
        """ test invalid POST with bad request 400 """

        data = {
            'password': '******',
        }

        res = client.post('/api/users/login',
                          data=json.dumps(data),
                          content_type='application/json')

        if res.status_code != 400:
            raise ValueError(
                'The res.status_code must be 401, please check your code')

    def test_users_post(self, client):
        data = {
            'username': '******',
            'email': '*****@*****.**',
            'password': '******',
            'gender': False,
            'fullname': 'radina yeah',
            'address': 'jombang',
            'phone': '08222221565637'
        }

        res = client.post('/api/users/register',
                          data=json.dumps(data),
                          content_type='application/json')

        if res.status_code != 200:
            raise ValueError(
                'The res.status_code must be 200, please check your code')

    def test_users_invalid_post(self, client):
        """ test invalid POST with bad request 400 """

        data = {
            'username': '******',
            'password': '******',
            'gender': False,
            'fullname': 'radina yeah'
        }

        res = client.post('/api/users/register', data=json.dumps(data))

        assert res.status_code == 400
        # if res.status_code != 400:
        # raise ValueError('The res.status_code must be 400, please check your code')

    def test_users_after_login(self, client):
        token = create_token1()

        res = client.get('/api/users/after_first_login',
                         headers={'Authorization': 'Bearer ' + token},
                         content_type='application/json')

        if res.status_code != 200:
            raise ValueError(
                'The res.status_code must be 200, please check your code')

    def test_users_after_login_invalid(self, client):
        token = create_token1()

        res = client.get('/api/users/after_first_login',
                         content_type='application/json')

        if res.status_code != 401:
            raise ValueError(
                'The res.status_code must be 401, please check your code')

    def test_users_post_forgot_password(self, client):
        data = {'email': '*****@*****.**', 'new_password': '******'}

        res = client.post('/api/users/add_new_password',
                          data=json.dumps(data),
                          content_type='application/json')

        if res.status_code != 200:
            raise ValueError(
                'The res.status_code must be 200, please check your code')

    def test_users_post_forgot_password_invalid_email(self, client):
        data = {"email": "*****@*****.**", "new_password": "******"}

        res = client.post('/api/users/add_new_password',
                          data=json.dumps(data),
                          content_type='application/json')

        assert res.status_code == 401
        # if res.status_code != 401:
        #     raise ValueError('The res.status_code must be 200, please check your code')

    def test_users_post_forgot_password_invalid_email(self, client):
        data = {'new_password': '******'}

        res = client.post('/api/users/add_new_password',
                          data=json.dumps(data),
                          content_type='application/json')

        assert res.status_code == 400

    def test_users_post_google_login(self, client):
        data = {"email": "*****@*****.**", "token_google": "vxvxvx"}

        res = client.post('/api/users/google_login',
                          data=json.dumps(data),
                          content_type='application/json')

        assert res.status_code == 200

    def test_users_post_google_login_invalid_email(self, client):
        data = {"email": "*****@*****.**", "token_google": "vxvxvx"}

        res = client.post('/api/users/google_login',
                          data=json.dumps(data),
                          content_type='application/json')

        assert res.status_code == 401

    def test_users_post_google_registration(self, client):
        data = {
            'username': '******',
            'email': '*****@*****.**',
            'password': '******',
            'gender': False,
            'fullname': 'radina yeah',
            'address': 'jombang',
            'phone': '08222221565637'
        }

        res = client.post('/api/users/register_with_google',
                          data=json.dumps(data),
                          content_type='application/json')

        assert res.status_code == 200

    def test_users_post_google_registration_invalid(self, client):
        data = {
            'username': '******',
            'email': 'maharraden765gmail.com',
            'password': '******',
            'gender': False,
            'fullname': 'radina yeah',
            'address': 'jombang',
            'phone': '08222221565637'
        }

        res = client.post('/api/users/register_with_google',
                          data=json.dumps(data),
                          content_type='application/json')

        assert res.status_code == 400
Exemplo n.º 9
0
class TestThirdPartyCrud():
    """class to test third party api"""

    reset_database()

    def test_zomato_get_valid(self, client):
        """class to test third party api zomato"""

        token = create_token()
        res = client.get('/api/recommendation/eat/2',
                         headers={'Authorization': 'Bearer ' + token},
                         content_type='application/json')

        if res.status_code != 200:
            raise ValueError(
                'The res.status_code must be 200, please check your code')

    def test_vacation_get_valid(self, client):
        """class to test third party api vacation"""

        token = create_token()
        res = client.get('/api/recommendation/vacation/2',
                         headers={'Authorization': 'Bearer ' + token},
                         content_type='application/json')

        if res.status_code != 200:
            raise ValueError(
                'The res.status_code must be 200, please check your code')

    def test_hiking_get_valid(self, client):
        """class to test third party api zomato"""

        token = create_token()
        res = client.get('/api/recommendation/hiking/2',
                         headers={'Authorization': 'Bearer ' + token},
                         content_type='application/json')

        if res.status_code != 200:
            raise ValueError(
                'The res.status_code must be 200, please check your code')

    @patch.object(RecommendationPlaceToEat, 'get')
    def testGetZomatoMock(self, mock_get):
        """class to test third party api zomato with mock"""
        response = {
            "restaurants":
            "WAKI Japanese BBQ Dining",
            "address":
            "Lantai 1, Jl. Tanjung Karang No. 5, Thamrin, Jakarta",
            "photo":
            "https://b.zmtcdn.com/data/reviews_photos/621/2623a19855d87924b3e4271473d8c621_1566921104.jpg?fit=around%7C200%3A200&crop=200%3A200%3B%2A%2C%2A"
        }

        mock_get.return_value = response

        assert RecommendationPlaceToEat.get(
            'https://developers.zomato.com/api/v2.1/search?b875502a178dcc62abd9f3437d92fbe5'
        ) == response

    @patch.object(RecommendationPlaceToVacation, 'get')
    def testGetVacationMock(self, mock_get):
        """class to test third party api vacation with mock"""

        response = {
            "place":
            "Candi Borobudur",
            "place_location":
            "Magelang",
            "photo":
            "https://b.zmtcdn.com/data/reviews_photos/621/2623a19855d87924b3e4271473d8c621_1566921104.jpg?fit=around%7C200%3A200&crop=200%3A200%3B%2A%2C%2A"
        }

        mock_get.return_value = response

        assert RecommendationPlaceToVacation.get(
            'https://opentripmap-places-v1.p.rapidapi.com/en/places/bbox?lon_min=107&lon_max=113&lat_min=-7&lat_max=-2'
        ) == response

    @patch.object(RecommendationPlaceToHike, 'get')
    def testGetHikingMock(self, mock_get):
        """class to test third party api hiking with mock"""

        response = {
            "place":
            "Gunung Salak",
            "place_location":
            "Bogor",
            "photo":
            "https://b.zmtcdn.com/data/reviews_photos/621/2623a19855d87924b3e4271473d8c621_1566921104.jpg?fit=around%7C200%3A200&crop=200%3A200%3B%2A%2C%2A"
        }

        mock_get.return_value = response

        assert RecommendationPlaceToHike.get(
            'https://opentripmap-places-v1.p.rapidapi.com/en/places/bbox?lon_min=107&lon_max=113&lat_min=-7&lat_max=-2'
        ) == response
Exemplo n.º 10
0
class TestUserPreferencesCrud():

    """
    class to test user preferences resource
    """

    reset_database()

    def test_user_preferences_post_valid(self, client):
        token = create_token()
        data = {
            "event_id" : 1,
            "preference": "cultural"
        }
        res = client.post('/api/users/preferences', data=json.dumps(data),
                        headers={'Authorization':'Bearer ' + token},
                        content_type='application/json')
        if res.status_code != 200:
            raise ValueError('The res.status_code must be 200, please check your code')
    
    def test_user_preferences_post_invalid_no_token(self, client):
        data = {
            "event_id" : 1,
            "preference": "hambar"
        }
        res = client.post('/api/users/preferences', data=json.dumps(data),
                        content_type='application/json')
        if res.status_code != 401:
            raise ValueError('The res.status_code must be 401, please check your code')
    
    def test_user_preferences_get_valid(self, client):
        token = create_token()
        res = client.get('/api/users/preferences/1',
                        headers={'Authorization':'Bearer ' + token},
                        content_type='application/json')
        if res.status_code != 200:
            raise ValueError('The res.status_code must be 200, please check your code')
    
    def test_user_preferences_get_invalid_no_token(self, client):
        res = client.get('/api/users/preferences/1',
                        content_type='application/json')
        if res.status_code != 401:
            raise ValueError('The res.status_code must be 401, please check your code')

    
    def test_user_preferences_put_valid(self, client):
        token = create_token()
        data = {
            "preference": "dinner"
        }
        res = client.put('/api/users/preferences/1', data=json.dumps(data),
                        headers={'Authorization':'Bearer ' + token},
                        content_type='application/json')
        if res.status_code != 200:
            raise ValueError('The res.status_code must be 200, please check your code')
    
    def test_user_preferences_put_invalid_token(self, client):
        data = {
            "preference": "dinner"
        }
        res = client.put('/api/users/preferences/1', data=json.dumps(data),
                        content_type='application/json')
        if res.status_code != 401:
            raise ValueError('The res.status_code must be 401, please check your code')

    def test_user_get_confirmation_valid(self, client):
        token = create_token()
        res = client.get('/api/users/preferences/confirmations/1',
                        headers={'Authorization':'Bearer ' + token},
                        content_type='application/json')
        if res.status_code != 200:
            raise ValueError('The res.status_code must be 200, please check your code')
    
    def test_user_get_confirmation_no_token(self, client):
        res = client.get('/api/users/preferences/confirmations/1',
                        content_type='application/json')
        if res.status_code != 401:
            raise ValueError('The res.status_code must be 200, please check your code')
    
    def test_user_preferences_put_confirmation_valid(self, client):
        token = create_token()
        data = {
            "confirmation" : 1
        }
        res = client.put('/api/users/preferences/confirmations/1', data=json.dumps(data),
                        headers={'Authorization':'Bearer ' + token},
                        content_type='application/json')
        if res.status_code != 200:
            raise ValueError('The res.status_code must be 200, please check your code')
    

    def test_user_preferences_put_confirmation_invalid_no_token(self, client):
        data = {
            "confirmation" : 1
        }
        res = client.put('/api/users/preferences/confirmations/1', data=json.dumps(data),
                        content_type='application/json')
        if res.status_code != 401:
            raise ValueError('The res.status_code must be 401, please check your code')
Exemplo n.º 11
0
class TestUserEndpoint():

    reset_database()

    def test_user_option1(self,client):
        res = client.options('/user/addnew')
        assert res.status_code == 200

    def test_user_option2(self,client):
        res = client.options('/user/me')
        assert res.status_code == 200

    def test_user_signup(self,client):
        data = {
            "name": "unittesting",
            "username": "******",
            "password": "******"
        }
        res = client.post('/user/addnew', data=json.dumps(data),content_type='application/json')
        res_json = json.loads(res.data)
        assert res.status_code == 200

    def test_user_signup_failed(self,client):
        data = {
            "name": "unittesting",
            "username": "******"
        }
        res = client.post('/user/addnew', data=json.dumps(data),content_type='application/json')
        res_json = json.loads(res.data)
        assert res.status_code == 400

    def test_user_getme(self,client):
        token=shop_required()
        res = client.get('/user/me',headers={'Authorization':'Bearer '+token})
        res_json = json.loads(res.data)
        assert res.status_code == 200

    def test_user_getme_failed(self,client):
        token=shop_required()
        res = client.get('/user/me',headers={'Authorization':'Bearer '+'abc'})
        res_json = json.loads(res.data)
        assert res.status_code == 422

    def test_user_editme(self,client):
        token=shop_required()
        data = {
            "name": "unittesting",
            "username": "******",
            "password": "******"
        }
        res = client.put('/user/me',headers={'Authorization':'Bearer '+token}, data=json.dumps(data),content_type='application/json')
        res_json = json.loads(res.data)
        assert res.status_code == 200

    def test_user_editme_failed(self,client):
        token=shop_required()
        data = {
            "name": "unittesting",
            "username": "******",
            "password": "******"
        }
        res = client.put('/user/me',headers={'Authorization':'Bearer '+'abc'}, data=json.dumps(data),content_type='application/json')
        res_json = json.loads(res.data)
        assert res.status_code == 422
Exemplo n.º 12
0
class TestProduct():

    reset_database()

    token = get_token_admin()

    def test_product(self, client):

        # add product type first
        data = {"name": "vehicle"}

        # test login admin

        res = client.post('/product_type/',
                          data=json.dumps(data),
                          headers={'Authorization': 'Bearer ' + self.token},
                          content_type='application/json')

        res_json = json.loads(res.data)

        assert res.status_code == 200
        id_product_type = res_json['id']

        data = {
            "name": "car",
            "product_type_id": id_product_type,
            "created_by": 1
        }

        # test login admin

        res = client.post('/product/',
                          data=json.dumps(data),
                          headers={'Authorization': 'Bearer ' + self.token},
                          content_type='application/json')

        res_json = json.loads(res.data)

        assert res.status_code == 200
        id_product = res_json['id']

        # test get product

        res = client.get('/product/' + str(id_product),
                         data=json.dumps(data),
                         headers={'Authorization': 'Bearer ' + self.token},
                         content_type='application/json')

        res_json = json.loads(res.data)

        assert res.status_code == 200

        data = {"name": "bus", "product_type_id": id_product_type}

        # test login admin

        res = client.put('/product/' + str(id_product),
                         data=json.dumps(data),
                         headers={'Authorization': 'Bearer ' + self.token},
                         content_type='application/json')

        res_json = json.loads(res.data)

        assert res.status_code == 200
Exemplo n.º 13
0
class TestInvitationsCrud():

    """
    class to test invitations resource
    """

    reset_database()

    def test_invitations_get_valid(self, client):
        token = create_token()
        res = client.get('/api/invitations',
                        headers={'Authorization':'Bearer ' + token},
                        content_type='application/json')
        res_json = json.loads(res.data)
        if res.status_code != 200:
            raise ValueError('The res.status_code must be 200, please check your code')
    
    def test_invitations_get_none(self, client):
        token = create_token2()
        res = client.get('/api/invitations',
                        headers={'Authorization':'Bearer ' + token},
                        content_type='application/json')
        res_json = json.loads(res.data)
        if res.status_code != 200:
            raise ValueError('The res.status_code must be 200, please check your code')

    def test_invitations_get_invalid_no_token(self, client):
        """
        test invalid get without token
        """

        res = client.get('/api/invitations',
                        content_type='application/json')
        res_json = json.loads(res.data)
        if res.status_code != 401:
            raise ValueError('The res.status_code must be 401, please check your code')
    
    def test_invitations_post_valid(self, client):
        token = create_token()
        data = {
            "invited_id": 5
        }
        res = client.post('/api/invitations', data=json.dumps(data),
                        headers={'Authorization':'Bearer ' + token},
                        content_type='application/json')
        res_json = json.loads(res.data)
        if res.status_code != 200:
            raise ValueError('The res.status_code must be 200, please check your code')
    
    def test_invitations_post_invalid_no_token(self, client):
        """
        test invalid post without token
        """
        
        data = {
            "event_id" : 3,
            "invited_id": 2
        }
        res = client.post('/api/invitations', data=json.dumps(data),
                        content_type='application/json')
        res_json = json.loads(res.data)
        if res.status_code != 401:
            raise ValueError('The res.status_code must be 401, please check your code')
    
    def test_invitations_accept_valid(self, client):
        token = create_token()
        res = client.put('/api/invitations/accept/1',
                        headers={'Authorization':'Bearer ' + token},
                        content_type='application/json')
        res_json = json.loads(res.data)
        if res.status_code != 200:
            raise ValueError('The res.status_code must be 200, please check your code')
    
    def test_invitations_accept_invalid(self, client):
        token = create_token2()
        res = client.put('/api/invitations/accept/1',
                        headers={'Authorization':'Bearer ' + token},
                        content_type='application/json')
        res_json = json.loads(res.data)
        if res.status_code != 404:
            raise ValueError('The res.status_code must be 404, please check your code')
    
    def test_invitations_accept_invalid_no_token(self, client):
        """
        test invalid accept without token
        """
        
        res = client.put('/api/invitations/accept/1',
                        content_type='application/json')
        res_json = json.loads(res.data)
        if res.status_code != 401:
            raise ValueError('The res.status_code must be 401, please check your code')
    
    def test_invitations_reject_valid(self, client):
        token = create_token1()
        res = client.put('/api/invitations/reject/3',
                        headers={'Authorization':'Bearer ' + token},
                        content_type='application/json')
        res_json = json.loads(res.data)
        if res.status_code != 200:
            raise ValueError('The res.status_code must be 200, please check your code')
    
    def test_invitations_reject_invalid(self, client):
        token = create_token2()
        res = client.put('/api/invitations/reject/2',
                        headers={'Authorization':'Bearer ' + token},
                        content_type='application/json')
        res_json = json.loads(res.data)
        if res.status_code != 404:
            raise ValueError('The res.status_code must be 404, please check your code')
    
    def test_invitations_reject_invalid_no_token(self, client):
        """
        test invalid reject without token
        """
        
        res = client.put('/api/invitations/reject/2',
                        content_type='application/json')
        res_json = json.loads(res.data)
        if res.status_code != 401:
            raise ValueError('The res.status_code must be 401, please check your code')

    def test_invitations_user_decline_event(self, client):
        """
        test invalid reject without token
        """
        token = create_token()
        res = client.delete('/api/invitations/decline/1',
                        headers={'Authorization':'Bearer ' + token},
                        content_type='application/json')

        if res.status_code != 200:
            raise ValueError('The res.status_code must be 200, please check your code')    


    def test_invitations_user_invalid_decline_event(self, client):
        """
        test invalid reject without token
        """
        token = create_token2()
        res = client.delete('/api/invitations/decline/3',
                headers={'Authorization':'Bearer ' + token},
                content_type='application/json')
        
        assert res.status_code == 404
        if res.status_code != 404:
            raise ValueError('The res.status_code must be 404, please check your code')    
Exemplo n.º 14
0
class TestDatesCrud():
    """class to test dates resource"""

    reset_database()

    def test_dates_post_valid(self, client):
        token = create_token()
        data = {"date": "20-01-2020"}
        res = client.post('/api/date',
                          data=json.dumps(data),
                          headers={'Authorization': 'Bearer ' + token},
                          content_type='application/json')
        res_json = json.loads(res.data)
        if res.status_code != 200:
            raise ValueError(
                'The res.status_code must be 200, please check your code')

    def test_dates_post_invalid_no_token(self, client):
        """test invalid post without token"""

        data = {"date": "11-12-2019"}
        res = client.post('/api/date',
                          data=json.dumps(data),
                          content_type='application/json')
        res_json = json.loads(res.data)
        if res.status_code != 401:
            raise ValueError(
                'The res.status_code must be 401, please check your code')

    def test_dates_delete_valid(self, client):
        token = create_token()
        data = {"date": "20-01-2020"}
        res = client.delete('/api/date',
                            data=json.dumps(data),
                            headers={'Authorization': 'Bearer ' + token},
                            content_type='application/json')
        res_json = json.loads(res.data)
        if res.status_code != 200:
            raise ValueError(
                'The res.status_code must be 404, please check your code')

    def test_dates_delete_invalid(self, client):
        token = create_token()
        data = {"date": "22-01-2020"}
        res = client.delete('/api/date',
                            data=json.dumps(data),
                            headers={'Authorization': 'Bearer ' + token},
                            content_type='application/json')
        res_json = json.loads(res.data)
        if res.status_code != 404:
            raise ValueError(
                'The res.status_code must be 200, please check your code')

    def test_dates_delete_invalid_no_token(self, client):
        """test invalid post without token"""

        data = {"date": "11-12-2019"}
        res = client.delete('/api/date',
                            data=json.dumps(data),
                            content_type='application/json')
        res_json = json.loads(res.data)
        if res.status_code != 401:
            raise ValueError(
                'The res.status_code must be 401, please check your code')

    def test_dates_get_valid(self, client):
        token = create_token()
        res = client.get('/api/date',
                         headers={'Authorization': 'Bearer ' + token},
                         content_type='application/json')
        res_json = json.loads(res.data)
        if res.status_code != 200:
            raise ValueError(
                'The res.status_code must be 200, please check your code')

    def test_dates_get_invalid_no_token(self, client):
        """test invalid post without token"""

        res = client.delete('/api/date', content_type='application/json')
        res_json = json.loads(res.data)
        if res.status_code != 401:
            raise ValueError(
                'The res.status_code must be 401, please check your code')
class TestUser():

    reset_database()

    def test_admin(self, client):

        #test register admin

        data = {
            "username": "******",
            "password": "******",
            "phonenumber": "0987635343",
            "address": "Bimasakti",
            "role": "admin"
        }

        res = client.post('/user/register',
                          data=json.dumps(data),
                          content_type='application/json')

        res_json = json.loads(res.data)
        assert res.status_code == 200
        assert res_json['data']['username'] == 'admin'

        ################
        #test login admin
        ################

        # 1 success

        data = {"username": "******", "password": "******"}

        res = client.post('/user/login',
                          data=json.dumps(data),
                          content_type='application/json')

        res_json = json.loads(res.data)

        assert res.status_code == 200
        assert res_json['token'] is not None
        token_admin = res.json['token']

        # 2 failed wrong password

        data = {"username": "******", "password": "******"}

        res = client.post('/user/login',
                          data=json.dumps(data),
                          content_type='application/json')

        assert res.status_code == 422

        # test create user by admin

        data = {
            "username": "******",
            "password": "******",
            "phonenumber": "987645343",
            "address": "Jember"
        }

        res = client.post('/user/',
                          data=json.dumps(data),
                          headers={'Authorization': 'Bearer ' + token_admin},
                          content_type='application/json')

        res_json = json.loads(res.data)

        assert res.status_code == 200
        id_user_test = res_json['data']['id']

        # test create user failed (user already exist)

        data = {
            "username": "******",
            "password": "******",
            "phonenumber": "987645343",
            "address": "Jember"
        }

        res = client.post('/user/',
                          data=json.dumps(data),
                          headers={'Authorization': 'Bearer ' + token_admin},
                          content_type='application/json')

        res_json = json.loads(res.data)

        assert res.status_code == 422

        # test update user by admin

        data = {
            "username": "******",
            "password": "******",
            "phonenumber": "987645343",
            "address": "Malang"
        }

        res = client.put('/user/' + str(id_user_test),
                         data=json.dumps(data),
                         headers={'Authorization': 'Bearer ' + token_admin},
                         content_type='application/json')

        assert res.status_code == 200

        ################
        # test get user
        ################

        # 1 failed, id is empty

        res = client.get('/user/',
                         data=json.dumps(data),
                         headers={'Authorization': 'Bearer ' + token_admin},
                         content_type='application/json')

        assert res.status_code == 400

        # 2 success

        res = client.get('/user/' + str(id_user_test),
                         data=json.dumps(data),
                         headers={'Authorization': 'Bearer ' + token_admin},
                         content_type='application/json')

        assert res.status_code == 200

        #test delete user

        res = client.delete('/user/' + str(id_user_test),
                            data=json.dumps(data),
                            headers={'Authorization': 'Bearer ' + token_admin},
                            content_type='application/json')

        assert res.status_code == 200
class TestUser():

    reset_database()

    def test_product_type(self, client):

        # test register admin
        data = {
            "username": "******",
            "password": "******",
            "phonenumber": "0123456789",
            "address": "Malang",
            "role": "admin"
        }

        res = client.post('/user/register',
                          data=json.dumps(data),
                          content_type='application/json')

        res_json = json.loads(res.data)
        assert res.status_code == 200
        assert res_json['data']['username'] == 'admin-trya'

        data = {"username": "******", "password": "******"}

        # test login admin

        res = client.post('/user/login',
                          data=json.dumps(data),
                          content_type='application/json')

        res_json = json.loads(res.data)

        assert res.status_code == 200
        assert res_json['token'] is not None
        token = res.json['token']

        # add
        data = {"name": "makanan"}

        res = client.post('/product_type/',
                          data=json.dumps(data),
                          headers={'Authorization': 'Bearer ' + token},
                          content_type='application/json')

        res_json = json.loads(res.data)
        id = res_json['id']
        assert res.status_code == 200
        assert res_json['name'] == 'makanan'

        # edit
        data = {"name": "food"}

        res = client.put('/product_type/' + str(id),
                         data=json.dumps(data),
                         headers={'Authorization': 'Bearer ' + token},
                         content_type='application/json')

        res_json = json.loads(res.data)
        assert res.status_code == 200
        assert res_json['name'] == 'food'

        # view list
        data = {}
        res = client.post('/product_type/list',
                          data=json.dumps(data),
                          headers={'Authorization': 'Bearer ' + token},
                          content_type='application/json')

        res_json = json.loads(res.data)
        assert res.status_code == 200
Exemplo n.º 17
0
class TestBuyerDetailsCrud():

    reset_database()

    ######### options
    def test_buyer_option1(self, client):
        res = client.options('profile')
        assert res.status_code == 200

    def test_buyer_option2(self, client):
        res = client.options('signup')
        assert res.status_code == 200

######### get profile

    def test_buyer_details_profile(self, client):
        token = create_token_buyer()
        res = client.get('profile',
                         headers={'Authorization': 'Bearer ' + token})

        res_json = json.loads(res.data)
        assert res.status_code == 200

    def test_buyer_details_invalid_profile(self, client):
        token = create_token_invalid()
        res = client.get('profile',
                         headers={'Authorization': 'Bearer ' + token})
        res_json = json.loads(res.data)
        assert res.status_code == 403

######### post

    def test_buyer_sign_up(self, client):
        random_number = int(random.random() * 1000)
        data = {
            "client_key": "Buyer Tes " + str(random_number),
            "client_secret": "passwordbuyer" + str(random_number),
            "name": "Buyer " + str(random_number),
            "email": "buyer" + str(random_number) + "@yahoo.com",
            "phone_number": "082buyer" + str(random_number),
            "address": "Alamat " + str(random_number),
            "postal_code": "4061" + str(random_number)
        }
        res = client.post('signup',
                          data=json.dumps(data),
                          content_type='application/json')

        res_json = json.loads(res.data)

        assert res.status_code == 200

    def test_buyer_invalid_sign_up(self, client):
        data = {
            "client_key": "buyer_1",
            "client_secret": "buyer1",
            "name": "Buyer 1",
            "email": "*****@*****.**",
            "phone_number": "082buyer1",
            "address": "Alamat 1",
            "postal_code": "40611"
        }
        res = client.post('signup',
                          data=json.dumps(data),
                          content_type='application/json')

        res_json = json.loads(res.data)

        res_json = json.loads(res.data)
        assert res.status_code == 500

######### put

    def test_buyer_update(self, client):
        token = create_token_buyer()
        data = {
            "client_key": "buyer_2",
            "client_secret": "buyer1",
            "name": "Buyer 1",
            "email": "*****@*****.**",
            "phone_number": "082buyer1",
            "address": "Alamat 1",
            "postal_code": "40611"
        }
        res = client.put('profile',
                         headers={'Authorization': 'Bearer ' + token},
                         data=json.dumps(data),
                         content_type='application/json')

        res_json = json.loads(res.data)
        assert res.status_code == 200

    def test_buyer_invalid_update(self, client):
        token = create_token_seller()
        data = {
            "client_key": "buyer_1",
            "client_secret": "buyer1",
            "name": "Buyer 1",
            "email": "*****@*****.**",
            "phone_number": "082buyer1",
            "address": "Alamat 1",
            "postal_code": "40611"
        }
        res = client.put('profile',
                         headers={'Authorization': 'Bearer ' + token},
                         data=json.dumps(data),
                         content_type='application/json')

        res_json = json.loads(res.data)
        assert res.status_code == 403
Exemplo n.º 18
0
 def test_reset_database(self, client):
     reset_database()
     assert 1 == 1