Ejemplo n.º 1
0
def test_get_token(client):
    new_user = {"email": "*****@*****.**", "password": "******"}
    user_response = client.post("/api/v1/users", json=new_user)
    assert user_response.status_code == 201
    response = client.post("/api/v1/token", json=new_user)
    assert response.status_code == 200
    assert 'access_token' in response.json and response.json['access_token']
Ejemplo n.º 2
0
def test_goods_get(token):
    response = client.post(
        "/warehouses",
        json={"address": "somemagicaddress"},
        headers={"Authorization": token},
    )
    assert response.status_code == 200

    names_and_codes = ["a", "b", "c"]
    for nc in names_and_codes:
        good_data = {
            "name": nc,
            "code": nc,
            "warehouse_id": response.json()["id"],
        }
        response_ = client.post(
            "/goods",
            json=good_data,
            headers={"Authorization": token},
        )
        assert response_.status_code == 200

    query = {"page": len(names_and_codes) - 1, "per_page": 1}
    response = client.get("/goods",
                          params=query,
                          headers={"Authorization": token})
    query["total_pages"] = len(names_and_codes)

    assert len(response.json()["data"]) == query["per_page"]
    assert response.json()["pagination"] == query
Ejemplo n.º 3
0
def test_order_get(token):
    dc_resp = client.post(
        "/delivery_companies",
        json={
            "name": "somemagicaname",
            "price": 2.0
        },
        headers={"Authorization": token},
    )
    assert dc_resp.status_code == 200
    comp_id = dc_resp.json()["id"]

    wh_resp = client.post(
        "/warehouses",
        json={"address": "somemagicaddress"},
        headers={"Authorization": token},
    )
    assert wh_resp.status_code == 200
    wh_id = wh_resp.json()["id"]

    names_and_codes = ["a", "b", "c"]
    goods_ids = []
    for nc in names_and_codes:
        good_data = {
            "name": nc,
            "code": nc,
            "warehouse_id": wh_id,
        }
        response_ = client.post(
            "/goods",
            json=good_data,
            headers={"Authorization": token},
        )
        assert response_.status_code == 200
        goods_ids.append(response_.json()["id"])

    order_data = {
        "delivery_expected_at": int(datetime.now().timestamp() * 1000),
        "delivery_company_id": comp_id,
        "goods": [{
            "id": g_id,
            "quantity": 1
        } for g_id in goods_ids],
    }

    response = client.post(
        "/orders",
        json=order_data,
        headers={"Authorization": token},
    )
    order_data["user_id"] = _user_id_from_token(token)
    order_data["status"] = "created"
    json = response.json()
    assert response.status_code == 200
    assert json.pop("id", None) is not None
    assert json.pop("created_at", None) is not None
    assert json.pop("goods", None) is not None
    order_data.pop("goods")
    assert json == order_data
Ejemplo n.º 4
0
def test_email_already_exists(client):
    user = {"email": "*****@*****.**", "password": "******"}
    response = client.post('/api/v1/users', json=user)
    assert response.status_code == 201
    assert response.json['msg'] == "Successfully created new user"
    response = client.post('/api/v1/users', json=user)
    assert response.status_code == 400
    assert b"User with email already exists" in response.data
Ejemplo n.º 5
0
    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
Ejemplo n.º 6
0
def test_create_subject():
    with database_mock() as db:
        db: StandardDatabase
        insert_mocks(db, "users")

        with authed_request(client, "alice", ALICE_PASSWORD) as params:
            response = client.post(
                "/subjects/",
                json={
                    "color": "red",
                    "name": "Sciences de l'ingénieur",
                    "weight": 1.0,
                    "goal": 3.67e-1,
                },
                **params,
            )

            assert response.status_code == 201
            for key, value in {
                    "color": "red",
                    "name": "Sciences de l'ingénieur",
                    "slug": "sciences-de-l-ingenieur",
                    "weight": 1.0,
                    "goal": 3.67e-1,
                    "location": "",
            }.items():
                assert response.json()[key] == value

            assert "_key" in response.json().keys()

            assert db.collection("subjects").all().count() == 1
            assert db.collection("subjects").get(
                response.json()["_key"]) is not None
Ejemplo n.º 7
0
def test_auth_wrong_username():
    with database_mock() as mock:
        mock.collection("users").insert(mocks.users.alice.json(by_alias=True))
        response = client.post(
            "/auth/", data={"username": "******", "password": ALICE_PASSWORD},
        )
        assert response.status_code == 401
Ejemplo n.º 8
0
def test_create_grades():
    with database_mock() as db:
        db: StandardDatabase
        insert_mocks(db, "users")
        insert_mocks(db, "grades")

        with authed_request(client, "alice", ALICE_PASSWORD) as params:
            response = client.post(
                "/grades/",
                json={
                    "subject_key": mocks.subjects.sciences_de_l_ingénieur._key,
                    "title": "Cinématique",
                    "unit": 95,
                    "details": "Aliquip sit aute ea pariatur.",
                },
                **params,
            )

            assert response.status_code == 201
            for key, value in {
                    "subject_key": mocks.subjects.sciences_de_l_ingénieur._key,
                    "title": "Cinématique",
                    "unit": 95,
                    "details": "Aliquip sit aute ea pariatur.",
            }.items():
                assert response.json()[key] == value

            assert "_key" in response.json().keys()

            assert db.collection("grades").all().count() == 5
            assert db.collection("grades").get(
                response.json()["_key"]) is not None
Ejemplo n.º 9
0
def test_del_user_api_case2(admin_login):
    client, app = admin_login
    with app.app_context():
        id = current_user.id
    rv = client.post("/api/user/delete", data={"id": id})
    code = rv.status_code
    assert code == 401
Ejemplo n.º 10
0
def test_toc_import(client):
    r = client.post('/v1/toc', data={'file': 'data/toc_100.enex'})
    assert int(r.get_json()['added']) == 100

    entry = client.get('/v1/toc').get_json()[1]
    assert entry['guid'] == '2df0f7b4-bec0-4005-a5ec-ebc3633ac303'
    assert entry['title'] == 'Kubernetes ConfigMaps and Secrets'
Ejemplo n.º 11
0
 def test_cart_checkout(self,client):
     token=shop_required()
     res = client.post('/cart/checkout',headers={'Authorization':'Bearer '+token})
     res_json = json.loads(res.data)
     print(res_json)
     TestCartEndpoint.transactionid=res_json[0]["id"]
     assert res.status_code == 200
Ejemplo n.º 12
0
def test_add_container(authorization_header,
                       form_data: dict = None,
                       user_id: int = None,
                       file: bytes = None) -> dict:
    if not form_data:
        form_data = {
            "user_id": user_id,
            "name": 'Default Container',
            "description": 'A Default Container',
            "filename": 'Dockerfile',
            "is_input_container": False,
            "is_output_container": False,
            "is_shared": False
        }

    if not user_id:
        me = client.get('/user/me', headers=authorization_header)
        assert me.status_code == 200
        form_data['user_id'] = me.json()['id']

    form_data['file'] = file if file else (io.BytesIO(b"test bytes"),
                                           'test.jpg')
    response = client.post('/container/',
                           data=form_data,
                           headers=authorization_header)

    assert response.status_code == 200
    return response.json()
Ejemplo n.º 13
0
def test_login_failure():
    username = '******'
    password = '******'
    user = utils.create_local_user('test', username, password)

    response = client.post('/auth/token', data={'username': user.username, 'password': password})
    assert response.status_code == 200

    response = client.post('/auth/token', data={'username': user.username, 'password': password+'sds'})
    assert response.status_code == 401

    response = client.post('/auth/token', data={'username': user.username, 'password': password[:-2]})
    assert response.status_code == 401

    response = client.post('/auth/token', data={'username': user.username.replace('_', ''), 'password': password})
    assert response.status_code == 401
Ejemplo n.º 14
0
    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
Ejemplo n.º 15
0
    def testRefreshToken(self, client):
        """ test refreshing user's token"""
        token = createTokenUser()
        res = client.post('/v1/auth/refresh',
                          headers={'Authorization': 'Bearer ' + token})

        res_json = json.loads(res.data)
        assert res.status_code == 200
Ejemplo n.º 16
0
def get_auth_header(username: str, password: str) -> dict:
    response = client.post('/auth/token', data={'username': username, 'password': password})
    assert response.status_code == 200

    data = response.json()
    assert 'access_token' in data and type(token := data['access_token']) is str

    return {'Authorization': f'Bearer {token}'}
Ejemplo n.º 17
0
def test_update_user_admin_case1(admin_login, id, username):
    client, app = admin_login
    rv = client.post("/api/user/update", data={"username": username, "id": id})
    d = rv.get_json()
    assert d['status'] == 200
    with app.app_context():
        user = User.get_user_by_id(id)
        assert user.username == username
Ejemplo n.º 18
0
    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
Ejemplo n.º 19
0
    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
Ejemplo n.º 20
0
def test_update_user_admin_case2(admin_login, id, email):
    client, app = admin_login
    rv = client.post("/api/user/update", data={"email": email, "id": id})
    d = rv.get_json()
    assert d['status'] == 200
    with app.app_context():
        user = User.get_user_by_id(id)
        assert user.email == email
Ejemplo n.º 21
0
def create_user(valid_user):
    response = client.post(url_for('users_bp.users_create'), json=valid_user)

    yield response

    user = UsersModel.query.filter_by(email="*****@*****.**").first()
    db.session.delete(user)
    db.session.commit()
Ejemplo n.º 22
0
 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
Ejemplo n.º 23
0
def test_auth_post():
    response = client.post(
        "/users",
        json={
            "username": "******",
            "password": "******",
            "role": "admin",
        },
    )
    assert response.status_code == 200
    response = client.post("/auth",
                           json={
                               "username": "******",
                               "password": "******"
                           })
    assert response.status_code == 200
    assert "token" in response.json()
Ejemplo n.º 24
0
 def test_user_signin(self,client):
     data = {
         "username": "******",
         "password": "******"
     }
     res = client.post('/public/login', data=json.dumps(data),content_type='application/json')
     res_json = json.loads(res.data)
     assert res.status_code == 200
Ejemplo n.º 25
0
def __create_user(client,
                  user={
                      "email": "*****@*****.**",
                      "password": "******"
                  }):
    user_response = client.post("/api/v1/users", json=user)
    assert user_response.status_code == 201
    return user_response.json['data']
def test_if_delete_users_address_with_success(create_user, valid_address):
    # Creating address for testing
    client.post(url_for('users_address_bp.users_address_create', user_id=1),
                json=valid_address)

    response = client.delete(
        url_for('users_address_bp.users_address_delete',
                user_id=1,
                address_id=1))

    expected_return = {
        "status": "Success",
        "status_code": 200,
        "message": "Address deleted successfully",
    }

    assert response.status_code == 200
    assert json.loads(response.data) == expected_return
Ejemplo n.º 27
0
def test_create_project_response(test_db):
    params = {"name": "TEST_PROJECT_NAME"}
    response = client.post("/projects", json=params)

    assert response.status_code == 200
    assert response.json() == {
        "id": 1,
        "name": "TEST_PROJECT_NAME",
    }
Ejemplo n.º 28
0
 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')
Ejemplo n.º 29
0
def test_create_bond_invalid_lei(client):
    invalid_bond = bond.copy()
    invalid_bond['lei'] = "R0MUWSFPU8MPRO8K5P83zzz1"
    token = __create_user(client)['access_token']
    bond_response = client.post("/api/v1/bonds",
                                json=invalid_bond,
                                headers={'Authorization': token})
    assert bond_response.status_code == 400
    assert b"No legal entity exists for lei" in bond_response.data
Ejemplo n.º 30
0
def authorization_header():
    response = client.post('/auth/token',
                           data={
                               'username': TEST_USER.username,
                               'password': TEST_USER.password
                           })
    assert response.status_code == 200

    return {'Authorization': f'Bearer {response.json()["access_token"]}'}
Ejemplo n.º 31
0
def post_json(method, data=None):
    return client.post('/json/', query_string={'method': method},
                       data=data, content_type='application/json')
Ejemplo n.º 32
0
def test_post_url():
    resp = client.post('/')
    assert 200 == resp.status_code
Ejemplo n.º 33
0
def post_json(method, data=None):
    return client.post("/json/", query_string={"method": method}, data=data, content_type="application/json")