Ejemplo n.º 1
0
def test_list_paginated(app, create_user):
    user = create_user()

    # Change the pagination to 3 items
    old = app.config["TAGS_PER_PAGE"]
    app.config["TAGS_PER_PAGE"] = 3

    # Create 5 tags for the user
    for i in range(5):
        tag = UserTag(
            name=f"tag{i}",
            description="my description",
            color="#fff",
            user=user,
        )
        db.session.add(tag)
    db.session.commit()

    with app.test_request_context():
        tags = UserTagController.list_items({"user_id": user.id})
        assert len(tags) == 3
        assert [t.name for t in tags] == ["tag0", "tag1", "tag2"]

        tags = UserTagController.list_items({"user_id": user.id, "page": 2})
        assert len(tags) == 2
        assert [t.name for t in tags] == ["tag3", "tag4"]

        with pytest.raises(NotFound):
            UserTagController.list_items({"user_id": user.id, "page": 3})

    app.config["TAGS_PER_PAGE"] = old
Ejemplo n.º 2
0
def test_list(app, create_user):
    user1 = create_user("user1")
    user1.tags.append(
        UserTag(
            name="mytag",
            description="my description",
            color="#fff",
            user=user1,
        )
    )
    db.session.commit()

    # User1 has one tag
    with app.test_request_context():
        tags = UserTagController.list_items({"user_id": user1.id})

    assert len(tags) == 1
    assert tags[0].user.id == user1.id
    assert tags[0].name == "mytag"
    assert tags[0].description == "my description"
    assert tags[0].color == "#fff"

    # User2 has no tag
    user2 = create_user("user2")
    with app.test_request_context():
        tags = UserTagController.list_items({"user_id": user2.id})

    assert len(tags) == 0
Ejemplo n.º 3
0
 def _create_tag(name, description, color, username="******"):
     user = User.query.filter_by(username=username).first()
     tag = UserTag(
         name=name,
         description=description,
         color=color,
         user=user,
     )
     db.session.add(tag)
     db.session.commit()
     return tag
Ejemplo n.º 4
0
def test_filtered_by_tags(app, create_cve, create_user):
    cve_2018_18074 = create_cve("CVE-2018-18074")
    cve_2020_9392 = create_cve("CVE-2020-9392")
    cve_2020_26116 = create_cve("CVE-2020-26116")
    create_cve("CVE-2020-27781")

    user = create_user()
    user.tags = [
        UserTag(name="tag1", description="foo", color="#fff"),
        UserTag(name="tag2", description="foo", color="#fff"),
    ]

    db.session.add(CveTag(user_id=user.id, cve_id=cve_2018_18074.id, tags=["tag1"]))
    db.session.add(
        CveTag(user_id=user.id, cve_id=cve_2020_9392.id, tags=["tag1", "tag2"])
    )
    db.session.add(CveTag(user_id=user.id, cve_id=cve_2020_26116.id, tags=["tag2"]))
    db.session.commit()

    # Tag is not in user's list of tags
    with pytest.raises(NotFound):
        CveController.list_items({"user_id": user.id, "tag": "notfound"})
    with app.test_request_context():
        cves = CveController.list_items()
        assert sorted([cve.cve_id for cve in cves]) == [
            "CVE-2018-18074",
            "CVE-2020-26116",
            "CVE-2020-27781",
            "CVE-2020-9392",
        ]
        cves = CveController.list_items({"user_id": user.id, "tag": "tag1"})
        assert sorted([cve.cve_id for cve in cves]) == [
            "CVE-2018-18074",
            "CVE-2020-9392",
        ]
        cves = CveController.list_items({"user_id": user.id, "tag": "tag2"})
        assert sorted([cve.cve_id for cve in cves]) == [
            "CVE-2020-26116",
            "CVE-2020-9392",
        ]
Ejemplo n.º 5
0
def test_cve_tags(mock, create_cve, create_user):
    user = create_user()
    mock.return_value = user

    cve = create_cve("CVE-2018-18074")
    cve = Cve.query.first()
    assert not cve.tags

    user_tag = UserTag(name="tag1", description="foo", color="#fff")
    user.tags.append(user_tag)
    db.session.add(CveTag(user_id=user.id, cve_id=cve.id, tags=["tag1"]))
    db.session.commit()

    cve = Cve.query.first()
    assert len(cve.tags) == 1
    assert cve.tags[0].id == user_tag.id
Ejemplo n.º 6
0
def test_get(app, create_user):
    user = create_user()
    user.tags.append(
        UserTag(
            name="mytag",
            description="my description",
            color="#fff",
            user=user,
        )
    )
    db.session.commit()

    with app.test_request_context():
        tag = UserTagController.get({"user_id": user.id, "name": "mytag"})
        assert tag.name == "mytag"
        assert tag.user.id == user.id

        with pytest.raises(NotFound):
            UserTagController.get({"user_id": user.id, "name": "notfound"})
Ejemplo n.º 7
0
def test_list_tags(client, login):
    user = User.query.first()

    # Create 5 tags for the user
    for i in range(5):
        tag = UserTag(
            name=f"tag{i}",
            description="my description",
            color="#fff",
            user=user,
        )
        db.session.add(tag)
    db.session.commit()

    response = client.get("/account/tags")
    assert response.status_code == 200
    assert b"tag0" in response.data
    assert b"tag1" in response.data
    assert b"tag2" in response.data
    assert b"tag3" in response.data
    assert b"tag4" in response.data
Ejemplo n.º 8
0
def tags():
    tags, _, pagination = UserTagController.list({
        **request.args, "user_id":
        current_user.id
    })
    tag_form = TagForm()

    # Form has been submitted
    if request.method == "POST" and tag_form.validate():

        # Check if the tag doesn't already exist
        if UserTag.query.filter_by(user_id=current_user.id,
                                   name=tag_form.name.data).first():
            flash("This tag already exists.", "error")

        # Create the new tag
        else:
            tag = UserTag(
                user=current_user,
                name=tag_form.name.data,
                description=tag_form.description.data,
                color=tag_form.color.data,
            )
            db.session.add(tag)
            db.session.commit()

            flash(f"The tag {tag.name} has been successfully added.",
                  "success")
            return redirect(
                url_for("main.edit_tag",
                        tag=tag.name,
                        page=request.args.get("page")))

    return render_template(
        "profiles/tags.html",
        tags=tags,
        form=tag_form,
        pagination=pagination,
        mode="create",
    )