def test_check_if_user_is_superuser_normal_user(db: Session) -> None:
    username = random_email()
    password = random_lower_string()
    user_in = UserCreate(email=username, password=password)
    user = crud.user.create(db, obj_in=user_in)
    is_superuser = crud.user.is_superuser(user)
    assert is_superuser is False
def test_create_user(db: Session) -> None:
    email = random_email()
    password = random_lower_string()
    user_in = UserCreate(email=email, password=password)
    user = crud.user.create(db, obj_in=user_in)
    assert user.email == email
    assert hasattr(user, "hashed_password")
Example #3
0
def test_check_if_user_is_superuser():
    email = random_email()
    password = random_lower_string()
    user_in = UserCreate(email=email, password=password, is_superuser=True)
    user = crud.user.create(obj_in=user_in)
    is_superuser = crud.user.is_superuser(user)
    assert is_superuser is True
def test_check_if_user_is_active(db: Session) -> None:
    email = random_email()
    password = random_lower_string()
    user_in = UserCreate(email=email, password=password)
    user = crud.user.create(db, obj_in=user_in)
    is_active = crud.user.is_active(user)
    assert is_active is True
Example #5
0
def test_get_multi_form_input_table(db: Session, superuser: User) -> None:
    names = [random_lower_string() for i in range(10)]
    dates = [
        date(1985, 1, 1) + timedelta(days=randint(0, 9999)) for i in range(10)
    ]
    integers = [randint(0, 10000) for i in range(10)]
    form_input_creates = [{
        "name": names[i],
        "date_created": dates[i],
        "an_integer": integers[i]
    } for i in range(10)]
    form_input = crud.form_input.get_by_template_table_name(
        db, table_name="form_input_test_table")
    form_input_table_crud = crud.form_input.get_table_crud(db,
                                                           id=form_input.id)
    for form_input_create in form_input_creates:
        form_input_table_crud.create(db, obj_in=form_input_create)
    stored_form_inputs = form_input_table_crud.get_multi(db=db)
    for fic in form_input_creates:
        found_match = False
        for sfi in stored_form_inputs.records:
            name_match = fic["name"] == sfi.name
            date_match = fic["date_created"] == sfi.date_created
            integer_match = fic["an_integer"] == sfi.an_integer
            if name_match and date_match and integer_match:
                found_match = True
                break
        assert found_match
Example #6
0
def test_create_user_existing_username(superuser_token_headers):
    server_api = get_server_api()
    username = random_lower_string()
    # username = email
    password = random_lower_string()
    user_in = UserCreate(email=username, password=password)
    crud.user.create(db_session, obj_in=user_in)
    data = {"email": username, "password": password}
    r = requests.post(
        f"{server_api}{config.API_V1_STR}/users/",
        headers=superuser_token_headers,
        json=data,
    )
    created_user = r.json()
    assert r.status_code == 400
    assert "_id" not in created_user
Example #7
0
def test_create_form_input_interface_fail_duplicate_table_name(
        client: TestClient, superuser_token_headers: dict,
        db: Session) -> None:
    """
    Interface creation should fail if the template table_name already
    exists
    """
    name = random_lower_string()
    template = test_table_template().dict()
    form_input_in = schemas.FormInputCreate(name=name, template=template)
    crud.form_input.create(db=db, obj_in=form_input_in, created_by_id=1)
    data = {
        "name": name,
        "template": template,
    }
    response = client.post(
        f"{settings.API_V1_STR}/interfaces/form-inputs/",
        headers=superuser_token_headers,
        json=data,
    )
    content = response.json()
    assert response.status_code == 400
    assert content["detail"] == (
        "A form input interface with that table name already exists, "
        "rename your template table.")
Example #8
0
def create_embedding_model(db: Session,
                           *,
                           name: str = None) -> models.Embedding_Model:
    if not name:
        name = random_lower_string()
    embedding_model_in = EmbeddingModelCreate(name=name)
    return crud.embedding_model.create(db=db, obj_in=embedding_model_in)
def basic_job(db: Session,
              tmpdir: py.path.local) -> Generator[Dict, None, None]:
    random_project_name = utils.random_lower_string()
    job_input = JobCreate(
        import_service="git",
        import_url=f"https://github.com/wikifactory/{random_project_name}",
        export_service="wikifactory",
        export_url=f"https://wikifactory.com/@user/{random_project_name}",
    )
    db_job = crud.job.create(db, obj_in=job_input)

    db_job.path = os.path.join(tmpdir, str(db_job.id))

    # Copy the content of test_files/sample-project to that path
    current_dir = os.path.dirname(os.path.realpath(__file__))
    copy_tree(
        os.path.normpath(
            os.path.join(current_dir, "..", "test_files", "sample-project")),
        db_job.path,
    )

    yield {
        "job_input": job_input,
        "db_job": db_job,
        "project_name": random_project_name,
    }
    crud.job.remove(db, id=db_job.id)
def test_get_all_permissions_for_user_group(db: Session,
                                            normal_user: User) -> None:
    nodes = [
        create_random_node(
            db,
            created_by_id=normal_user.id,
            node_type="test_get_all_permissions_for_user_group",
        ) for n in range(10)
    ]
    name = random_lower_string()
    user_group_in = UserGroupCreate(name=name, node_id=random.choice(nodes).id)
    user_group = crud.user_group.create(db=db,
                                        obj_in=user_group_in,
                                        created_by_id=normal_user.id)
    permissions = chain(
        *[crud.node.get_permissions(db, id=node.id) for node in nodes])
    for permission in permissions:
        crud.permission.grant(db,
                              user_group_id=user_group.id,
                              permission_id=permission.id)
    stored_permissions = crud.user_group.permissions_in_user_group(
        db=db, id=user_group.id)

    for permission in permissions:
        assert permission.id in [sp.id for sp in stored_permissions]
        assert permission.resource_id in [
            sp.resource_id for sp in stored_permissions
        ]
        assert permission.permission_type in [
            sp.permission_type for sp in stored_permissions
        ]
Example #11
0
def test_get_node_by_name(db: Session, superuser: User) -> None:
    node_in = NodeCreate(name=random_lower_string(), node_type="node")
    node = crud.node.create(db=db, obj_in=node_in, created_by_id=superuser.id)
    stored_node = crud.node.get_by_name(db=db, name=node.name)
    assert stored_node
    assert node.id == stored_node.id
    assert node.name == stored_node.name
Example #12
0
def create_random_fact(db: Session, user: models.User) -> models.Fact:
    text = random_lower_string()
    identifier = random_lower_string()
    answer = random_lower_string()
    deck = create_random_deck(db=db, user=user)
    deck_id = deck.id
    answer_lines = [answer]
    extra = {"type": "Noodles"}

    fact_in = FactCreate(text=text,
                         answer=answer,
                         deck_id=deck_id,
                         answer_lines=answer_lines,
                         identifier=identifier,
                         extra=extra)
    return crud.fact.create_with_owner(db=db, obj_in=fact_in, user=user)
def test_non_existent_store(client: TestClient, db: Session) -> None:
    non_existent_store_name = random_lower_string()

    response = client.get(
        f"{settings.API_V1_STR}/products_in_store?store_code={non_existent_store_name}",
    )
    assert response.status_code == 404
Example #14
0
def test_create_year(client: TestClient, superuser_token_headers: dict[str,
                                                                       str],
                     db: Session) -> None:
    school_id = create_random_school(db).id
    start_year = datetime.now().year
    end_year = start_year + 1
    name = random_lower_string()
    data = {
        "name": name,
        "school_id": school_id,
        "start_year": start_year,
        "end_year": end_year,
    }
    r = client.post(f"{settings.API_V1_STR}/years/",
                    headers=superuser_token_headers,
                    json=data)
    assert r.status_code == 200
    created_year = r.json()
    year = crud.year.get_by_details(db,
                                    name=name,
                                    school_id=school_id,
                                    start_year=start_year,
                                    end_year=end_year)
    assert year
    compare_api_and_db_query_results(api_result=created_year,
                                     db_dict=to_json(year))
    compare_api_and_db_query_results(data, created_year)
Example #15
0
def test_create_user_existing_username(client: TestClient, db: Session,
                                       superuser_token_headers):
    username = random_lower_string()
    # username = email
    password = random_lower_string()
    user_in = UserCreate(email=username, password=password)
    user = crud.user.create(db, user_in=user_in)
    data = {"email": username, "password": password}
    r = client.post(
        f"{settings.API_V1_STR}/users/",
        headers=superuser_token_headers,
        json=data,
    )
    created_user = r.json()
    assert r.status_code == 400
    assert "_id" not in created_user
def test_create_user_with_empty_password_is_not_allowed():
    username = random_lower_string()
    email = random_email()
    with pytest.raises(ValidationError):
        UserCreate(username=username, email=email, password=None)
    with pytest.raises(ValidationError):
        UnprivilegedUserCreate(username=username, email=email, password=None)
Example #17
0
def item() -> Item:
    title = random_lower_string()
    priority = 123
    user_id = "C@234"
    user_name = "user.name"
    item_in = ItemCreate(title=title, priority=priority, user_id=user_id, user_name=user_name)
    return crud.item.create(db_session=db_session, obj_in=item_in)
def test_create_user(db: Session) -> None:
    email = random_email()
    password = random_lower_string()
    user_in = UserCreate(email=email, password=password)
    user = services.user.create_user(user_in.to_dto())
    assert user.email == email
    assert hasattr(user, "password")
Example #19
0
def test_update_school(db: Session) -> None:
    school = create_random_school(db)
    name = random_lower_string()
    school_in = SchoolUpdate(name=name)
    school = crud.school.update(db=db, db_obj=school, obj_in=school_in)
    assert school
    assert school.name == name
def create_random_book(sql: Session) -> Books:
    """create a random book values"""
    title = random_lower_string()
    author = create_random_author(sql)

    book_in = BooksBase(Title=title, AuthorId=author.AuthorId)
    return create_book(sql, book=book_in)
Example #21
0
def test_create_user_by_normal_user():
    server_api = get_server_api()
    username = random_lower_string()
    password = random_lower_string()
    user_in = UserInCreate(username=username,
                           email=username,
                           password=password)
    bucket = get_default_bucket()
    user = upsert_user(bucket, user_in, persist_to=1)
    user_token_headers = user_authentication_headers(server_api, username,
                                                     password)
    data = {"username": username, "password": password}
    r = requests.post(f"{server_api}{config.API_V1_STR}/users/",
                      headers=user_token_headers,
                      json=data)
    assert r.status_code == 400
Example #22
0
def test_check_if_user_is_active_inactive():
    email = random_email()
    password = random_lower_string()
    user_in = UserCreate(email=email, password=password, disabled=True)
    user = crud.user.create(obj_in=user_in)
    is_active = crud.user.is_active(user)
    assert is_active
Example #23
0
def test_update_user(db: Session) -> None:
    email = random_email()
    password = random_lower_string()
    username = random_lower_string()
    user_in = UserCreate(email=email,
                         username=username,
                         password=password,
                         is_superuser=True)
    user = crud.user.create(db, obj_in=user_in)
    new_password = random_lower_string()
    user_in_update = UserUpdate(password=new_password, is_superuser=True)
    crud.user.update(db, db_obj=user, obj_in=user_in_update)
    user_2 = crud.user.get(db, id=user.id)
    assert user_2
    assert user.email == user_2.email
    assert verify_password(new_password, user_2.hashed_password)
Example #24
0
def test_create_user_existing_email(client: TestClient,
                                    superuser_token_headers: dict) -> None:
    full_name = random_lower_string()
    email = random_email()
    password = random_lower_string()
    user_in = UserCreate(full_name=full_name, email=email, password=password)
    crud.user.create(obj_in=user_in)
    data = {"full_name": full_name, "email": email, "password": password}
    r = client.post(
        f"{settings.API_V1_STR}/users/",
        headers=superuser_token_headers,
        json=data,
    )
    created_user = r.json()
    assert r.status_code == 400
    assert "_id" not in created_user
def test_get_user():
    password = random_lower_string()
    username = random_email()
    user_in = UserCreate(email=username, password=password, is_superuser=True)
    user = crud.user.create(db_session, obj_in=user_in)
    user_2 = crud.user.get(db_session, id=user.id)
    assert user.email == user_2.email
    assert jsonable_encoder(user) == jsonable_encoder(user_2)
Example #26
0
def test_create_user_existing_username(superuser_token_headers):
    server_api = get_server_api()
    username = random_lower_string()
    # username = email
    password = random_lower_string()
    user_in = UserCreate(username=username, email=username, password=password)
    bucket = get_default_bucket()
    user = crud.user.upsert(bucket, user_in=user_in, persist_to=1)
    data = {"username": username, "password": password}
    r = requests.post(
        f"{server_api}{config.API_V1_STR}/users/",
        headers=superuser_token_headers,
        json=data,
    )
    created_user = r.json()
    assert r.status_code == 400
    assert "_id" not in created_user
Example #27
0
def test_user_is_superuser_not(client: TestClient, event_loop: EventLoop):
    email = random_email()
    password_hash = random_lower_string()
    user = event_loop.run_until_complete(
        User.create(email=email,
                    password_hash=password_hash,
                    is_superuser=False))
    assert crud.user.is_superuser(user) == False
def test_create_user_existing_email(client: TestClient,
                                    superuser_token_headers: Dict[str, str],
                                    db: Session) -> None:
    username = random_lower_string()
    email = random_email()
    password = random_lower_string()
    user_in = UserCreate(email=email, password=password, username=username)
    crud.user.create(db, obj_in=user_in)
    data = {"email": email, "password": password, "username": username}
    r = client.post(
        f"{settings.API_V1_STR}/users/",
        headers=superuser_token_headers,
        json=data,
    )
    created_user = r.json()
    assert r.status_code == 400
    assert "_id" not in created_user
def test_update_item():
    title = random_lower_string()
    description = random_lower_string()
    item_in = ItemCreate(title=title, description=description)
    user = create_random_user()
    item = crud.item.create_with_owner(db_session=db_session,
                                       obj_in=item_in,
                                       owner_id=user.id)
    description2 = random_lower_string()
    item_update = ItemUpdate(description=description2)
    item2 = crud.item.update(db_session=db_session,
                             db_obj=item,
                             obj_in=item_update)
    assert item.id == item2.id
    assert item.title == item2.title
    assert item2.description == description2
    assert item.owner_id == item2.owner_id
Example #30
0
def test_get_product(client: TestClient, db: Session):
    name = random_lower_string()
    desc = random_lower_string()

    product = product_repo.create(db,
                                  obj_in=ProductCreate(name=name,
                                                       description=desc))

    data = {"name": name, "description": desc}

    response = client.get(f"{settings.API_PREFIX}/v1/products/{product.id}")

    assert response.status_code == 200
    content = response.json()

    assert content["name"] == name
    assert content["description"] == desc