def test_edit_user_tag_restrictions_with_prior_entries_test_removal_of_entry():
    # Setup
    user_id = db_utils.create_user()
    tags = searchtag.parse_restricted_tags(", ".join(combined_tags))
    searchtag.edit_user_tag_restrictions(user_id, tags)
    tags_to_keep = {'test', 'te*st', 'test_too'}

    # Set the new tags; AKA, remove the two defined tags
    tags = searchtag.parse_restricted_tags(", ".join(tags_to_keep))
    searchtag.edit_user_tag_restrictions(user_id, tags)
    resultant_tags = searchtag.query_user_restricted_tags(user_id)
    assert resultant_tags == tags_to_keep
Esempio n. 2
0
def test_edit_user_tag_restrictions_with_prior_entries_test_removal_of_entry():
    # Setup
    user_id = db_utils.create_user()
    tags = searchtag.parse_restricted_tags(", ".join(combined_tags))
    searchtag.edit_user_tag_restrictions(user_id, tags)
    tags_to_keep = {'test', 'te*st', 'test_too'}

    # Set the new tags; AKA, remove the two defined tags
    tags = searchtag.parse_restricted_tags(", ".join(tags_to_keep))
    searchtag.edit_user_tag_restrictions(user_id, tags)
    resultant_tags = set(searchtag.query_user_restricted_tags(user_id))
    assert resultant_tags == tags_to_keep
Esempio n. 3
0
def directorcontrol_globaltagrestrictions_post_(request):
    tags = searchtag.parse_restricted_tags(request.params["tags"])
    searchtag.edit_global_tag_restrictions(request.userid, tags)
    tags = searchtag.get_global_tag_restrictions(request.userid)
    return Response(
        d.webpage(request.userid, "directorcontrol/globaltagrestrictions.html",
                  (tags, )))
Esempio n. 4
0
def test_edit_user_tag_restrictions_fully_clear_entries_after_adding_items():
    user_id = db_utils.create_user()
    tags = searchtag.parse_restricted_tags(", ".join(combined_tags))
    searchtag.edit_user_tag_restrictions(user_id, tags)
    tags = set()
    searchtag.edit_user_tag_restrictions(user_id, tags)
    assert searchtag.query_user_restricted_tags(user_id) == []
Esempio n. 5
0
def test_attempt_setting_tags_when_some_tags_have_been_restricted():
    """
    Verify that tags are excluded from being added to a submission's tags if the tag is restricted
    """
    userid_owner = db_utils.create_user()
    userid_tag_adder = db_utils.create_user()
    journalid = db_utils.create_journal(userid_owner)
    charid = db_utils.create_character(userid_owner)
    submitid = db_utils.create_submission(userid_owner)
    restricted_tag = searchtag.parse_restricted_tags("pearl")
    searchtag.edit_user_tag_restrictions(userid_owner, restricted_tag)

    searchtag.associate(userid_tag_adder, tags, submitid=submitid)
    searchtag.associate(userid_tag_adder, tags, charid=charid)
    searchtag.associate(userid_tag_adder, tags, journalid=journalid)

    # Verify that the "pearl" tag was not added
    submission_tags = searchtag.select(submitid=submitid)
    assert tags_two == set(submission_tags)

    character_tags = searchtag.select(charid=charid)
    assert tags_two == set(character_tags)

    journal_tags = searchtag.select(journalid=journalid)
    assert tags_two == set(journal_tags)
def test_edit_user_tag_restrictions_fully_clear_entries_after_adding_items():
    user_id = db_utils.create_user()
    tags = searchtag.parse_restricted_tags(", ".join(combined_tags))
    searchtag.edit_user_tag_restrictions(user_id, tags)
    tags = set()
    searchtag.edit_user_tag_restrictions(user_id, tags)
    assert searchtag.query_user_restricted_tags(user_id) == set()
Esempio n. 7
0
def test_moderators_and_above_can_add_restricted_tags_successfully(monkeypatch):
    """
    Moderators (and admins, technical, and directors) can add restricted tags to content.
    Developers are not included in this test, as they are for all intents and purposes just
      normal user accounts.
    """
    userid_owner = db_utils.create_user()
    mod_tag_adder = db_utils.create_user()
    monkeypatch.setattr(staff, 'MODS', frozenset([mod_tag_adder]))
    journalid = db_utils.create_journal(userid_owner)
    charid = db_utils.create_character(userid_owner)
    submitid = db_utils.create_submission(userid_owner)
    restricted_tag = searchtag.parse_restricted_tags("pearl")
    searchtag.edit_user_tag_restrictions(userid_owner, restricted_tag)

    searchtag.associate(mod_tag_adder, tags, submitid=submitid)
    searchtag.associate(mod_tag_adder, tags, charid=charid)
    searchtag.associate(mod_tag_adder, tags, journalid=journalid)

    # Verify that all tags were added successfully. 'pearl' is restricted.
    submission_tags = searchtag.select(submitid=submitid)
    assert tags == set(submission_tags)

    character_tags = searchtag.select(charid=charid)
    assert tags == set(character_tags)

    journal_tags = searchtag.select(journalid=journalid)
    assert tags == set(journal_tags)
def test_parse_restricted_tags():
    """
    Ensure that ``searchtag.parse_restricted_tags()`` functions as expected.

    Examples of valid patterns include:
        - test*     (Wildcard at the end);
        - *test     (Wildcard at the start);
        - te*st     (Wildcard in the middle);
        - test      (A raw tag with no wildcard); and
        - test_too  (Underscores are also permitted)

    Examples of invalid patterns include:
        - *             (No raw wildcards)
        - a* or *a      (Must have at least two alphanumeric characters with a wildcard)
        - a*a*, *a*a, *aa*, or a**a  (Only one asterisk per tag)
        - }     Anything that wouldn't be returned by ``searchtag.parse_tags()``,
            since this function essentially reimplements and extends that function.
    """
    invalid_tags = {'*', '**', '***', 'a*', '*a', 'a*a*', '*a*a', '*aa*', 'a**a', '}'}
    combined_tags = valid_tags | invalid_tags

    # Function under test
    resultant_tags = searchtag.parse_restricted_tags(" ".join(combined_tags))

    # Verify that we have the tags in the valid list
    assert resultant_tags == valid_tags
def test_parse_restricted_tags():
    """
    Ensure that ``searchtag.parse_restricted_tags()`` functions as expected.

    Examples of valid patterns include:
        - test*     (Wildcard at the end);
        - *test     (Wildcard at the start);
        - te*st     (Wildcard in the middle);
        - test      (A raw tag with no wildcard); and
        - test_too  (Underscores are also permitted)

    Examples of invalid patterns include:
        - *             (No raw wildcards)
        - a* or *a      (Must have at least two alphanumeric characters with a wildcard)
        - a*a*, *a*a, *aa*, or a**a  (Only one asterisk per tag)
        - }     Anything that wouldn't be returned by ``searchtag.parse_tags()``,
            since this function essentially reimplements and extends that function.
    """
    invalid_tags = {
        '*', '**', '***', 'a*', '*a', 'a*a*', '*a*a', '*aa*', 'a**a', '}'
    }
    combined_tags = valid_tags | invalid_tags

    # Function under test
    resultant_tags = searchtag.parse_restricted_tags(" ".join(combined_tags))

    # Verify that we have the tags in the valid list
    assert resultant_tags == valid_tags
Esempio n. 10
0
def test_edit_global_tag_restrictions_fully_clear_entries_after_adding_items(monkeypatch):
    director_user_id = db_utils.create_user()
    monkeypatch.setattr(staff, 'DIRECTORS', frozenset([director_user_id]))
    tags = searchtag.parse_restricted_tags(", ".join(combined_tags))
    searchtag.edit_global_tag_restrictions(director_user_id, tags)
    tags = set()
    searchtag.edit_global_tag_restrictions(director_user_id, tags)
    assert searchtag.get_global_tag_restrictions(director_user_id) == []
def test_edit_global_tag_restrictions_fully_clear_entries_after_adding_items(monkeypatch):
    director_user_id = db_utils.create_user()
    monkeypatch.setattr(staff, 'DIRECTORS', frozenset([director_user_id]))
    tags = searchtag.parse_restricted_tags(", ".join(combined_tags))
    searchtag.edit_global_tag_restrictions(director_user_id, tags)
    tags = set()
    searchtag.edit_global_tag_restrictions(director_user_id, tags)
    assert searchtag.get_global_tag_restrictions(director_user_id) == {}
def test_edit_global_tag_restrictions(monkeypatch):
    director_user_id = db_utils.create_user(username="******")
    monkeypatch.setattr(staff, 'DIRECTORS', frozenset([director_user_id]))
    tags = searchtag.parse_restricted_tags(", ".join(combined_tags))
    searchtag.edit_global_tag_restrictions(director_user_id, tags)
    resultant_tags = searchtag.get_global_tag_restrictions(director_user_id)
    assert set(resultant_tags.keys()) == valid_tags
    assert set(resultant_tags.values()) == {"testdirector"}
def test_get_global_searchtag_restrictions(monkeypatch):
    director_user_id = db_utils.create_user(username="******")
    monkeypatch.setattr(staff, 'DIRECTORS', frozenset([director_user_id]))
    tags = searchtag.parse_restricted_tags(", ".join(combined_tags))
    searchtag.edit_global_tag_restrictions(director_user_id, tags)
    resultant_tags = searchtag.get_global_tag_restrictions(director_user_id)
    assert set(resultant_tags.keys()) == valid_tags
    assert set(resultant_tags.values()) == {"testdirector"}
Esempio n. 14
0
def control_tagrestrictions_post_(request):
    tags = searchtag.parse_restricted_tags(request.params["tags"])
    searchtag.edit_user_tag_restrictions(request.userid, tags)
    return Response(
        define.webpage(
            request.userid,
            "control/edit_tagrestrictions.html",
            (searchtag.query_user_restricted_tags(request.userid), ),
            title="Edit Community Tagging Restrictions"))
def test_edit_user_tag_restrictions_with_no_prior_entries():
    """
    Verify that we can successfully set new user restricted tags
    when no existing tags have been set for a given user previously.
    """
    user_id = db_utils.create_user()
    tags = searchtag.parse_restricted_tags(", ".join(combined_tags))
    searchtag.edit_user_tag_restrictions(user_id, tags)
    resultant_tags = searchtag.query_user_restricted_tags(user_id)
    assert resultant_tags == valid_tags
Esempio n. 16
0
def test_edit_user_tag_restrictions_with_no_prior_entries():
    """
    Verify that we can successfully set new user restricted tags
    when no existing tags have been set for a given user previously.
    """
    user_id = db_utils.create_user()
    tags = searchtag.parse_restricted_tags(", ".join(combined_tags))
    searchtag.edit_user_tag_restrictions(user_id, tags)
    resultant_tags = set(searchtag.query_user_restricted_tags(user_id))
    assert resultant_tags == valid_tags
Esempio n. 17
0
def test_get_global_searchtag_restrictions(monkeypatch):
    director_user_id = db_utils.create_user(username="******")
    monkeypatch.setattr(staff, 'DIRECTORS', frozenset([director_user_id]))
    tags = searchtag.parse_restricted_tags(", ".join(combined_tags))
    searchtag.edit_global_tag_restrictions(director_user_id, tags)
    resultant_tags = searchtag.get_global_tag_restrictions(director_user_id)
    resultant_tags_titles = [x.title for x in resultant_tags]
    assert resultant_tags_titles == valid_tags
    for user in resultant_tags:
        assert user.login_name == "testdirector"
def test_uppercase_tags_are_converted_to_lowercase():
    uppercase_tags = {
        'OMEGA_RUBY', 'ALPHA_SAPPHIRE', 'DIAMOND', 'PEARL', 'DIGI_*'
    }
    lowercase_tags = {
        'omega_ruby', 'alpha_sapphire', 'diamond', 'pearl', 'digi_*'
    }

    assert lowercase_tags == searchtag.parse_restricted_tags(
        " ".join(uppercase_tags))
Esempio n. 19
0
def test_get_global_searchtag_restrictions(monkeypatch):
    director_user_id = db_utils.create_user(username="******")
    monkeypatch.setattr(staff, 'DIRECTORS', frozenset([director_user_id]))
    tags = searchtag.parse_restricted_tags(", ".join(combined_tags))
    searchtag.edit_global_tag_restrictions(director_user_id, tags)
    resultant_tags = searchtag.get_global_tag_restrictions(director_user_id)
    resultant_tags_titles = [x.title for x in resultant_tags]
    assert resultant_tags_titles == valid_tags
    for user in resultant_tags:
        assert user.login_name == "testdirector"
Esempio n. 20
0
def test_get_global_searchtag_restrictions_fails_for_non_directors(
        monkeypatch):
    # Setup the globally restricted tags list
    director_user_id = db_utils.create_user()
    monkeypatch.setattr(staff, 'DIRECTORS', frozenset([director_user_id]))
    tags = searchtag.parse_restricted_tags(", ".join(combined_tags))
    searchtag.edit_global_tag_restrictions(director_user_id, tags)

    normal_user_id = db_utils.create_user()
    developer_user_id = db_utils.create_user()
    mod_user_id = db_utils.create_user()
    admin_user_id = db_utils.create_user()
    technical_user_id = db_utils.create_user()

    # Monkeypatch the staff global variables
    monkeypatch.setattr(staff, 'DEVELOPERS', frozenset([developer_user_id]))
    monkeypatch.setattr(staff, 'MODS', frozenset([mod_user_id]))
    monkeypatch.setattr(staff, 'ADMINS', frozenset([admin_user_id]))
    monkeypatch.setattr(staff, 'TECHNICAL', frozenset([technical_user_id]))

    with pytest.raises(WeasylError) as err:
        searchtag.get_global_tag_restrictions(normal_user_id)
    assert err.value.value == 'InsufficientPermissions'

    with pytest.raises(WeasylError) as err:
        searchtag.get_global_tag_restrictions(developer_user_id)
    assert err.value.value == 'InsufficientPermissions'

    with pytest.raises(WeasylError) as err:
        searchtag.get_global_tag_restrictions(mod_user_id)
    assert err.value.value == 'InsufficientPermissions'

    with pytest.raises(WeasylError) as err:
        searchtag.get_global_tag_restrictions(admin_user_id)
    assert err.value.value == 'InsufficientPermissions'

    with pytest.raises(WeasylError) as err:
        searchtag.get_global_tag_restrictions(technical_user_id)
    assert err.value.value == 'InsufficientPermissions'
Esempio n. 21
0
def test_get_global_searchtag_restrictions_fails_for_non_directors(monkeypatch):
    # Setup the globally restricted tags list
    director_user_id = db_utils.create_user()
    monkeypatch.setattr(staff, 'DIRECTORS', frozenset([director_user_id]))
    tags = searchtag.parse_restricted_tags(", ".join(combined_tags))
    searchtag.edit_global_tag_restrictions(director_user_id, tags)

    normal_user_id = db_utils.create_user()
    developer_user_id = db_utils.create_user()
    mod_user_id = db_utils.create_user()
    admin_user_id = db_utils.create_user()
    technical_user_id = db_utils.create_user()

    # Monkeypatch the staff global variables
    monkeypatch.setattr(staff, 'DEVELOPERS', frozenset([developer_user_id]))
    monkeypatch.setattr(staff, 'MODS', frozenset([mod_user_id]))
    monkeypatch.setattr(staff, 'ADMINS', frozenset([admin_user_id]))
    monkeypatch.setattr(staff, 'TECHNICAL', frozenset([technical_user_id]))

    with pytest.raises(WeasylError) as err:
        searchtag.get_global_tag_restrictions(normal_user_id)
    assert err.value.value == 'InsufficientPermissions'

    with pytest.raises(WeasylError) as err:
        searchtag.get_global_tag_restrictions(developer_user_id)
    assert err.value.value == 'InsufficientPermissions'

    with pytest.raises(WeasylError) as err:
        searchtag.get_global_tag_restrictions(mod_user_id)
    assert err.value.value == 'InsufficientPermissions'

    with pytest.raises(WeasylError) as err:
        searchtag.get_global_tag_restrictions(admin_user_id)
    assert err.value.value == 'InsufficientPermissions'

    with pytest.raises(WeasylError) as err:
        searchtag.get_global_tag_restrictions(technical_user_id)
    assert err.value.value == 'InsufficientPermissions'
Esempio n. 22
0
def test_associate_return_values():
    """
    ``associate()`` returns a dict, of the following format:
    return {"add_failure_restricted_tags": add_failure_restricted_tags,
            "remove_failure_owner_set_tags": remove_failure_owner_set_tags}
    /OR/ None

    add_failure_restricted_tags is None if no tags failed to be added during the associate call,
    when due to a tag being on the user or globally restricted tags list. Otherwise, it contains
    a space-separated list of tags which failed to be added to the content item.

    remove_failure_owner_set_tags is None if no tags failed to be removed during the associate call.
    Otherwise, it contains the same space-separated list as above, however containing tags which the
    content owner added and has opted to not permit others to remove.

    If neither element of the dict is set, ``associate()`` returns None.
    """
    config = CharSettings({'disallow-others-tag-removal'}, {}, {})
    userid_owner = db_utils.create_user(config=config)
    userid_tag_adder = db_utils.create_user()
    submitid = db_utils.create_submission(userid_owner)
    journalid = db_utils.create_journal(userid_owner)
    charid = db_utils.create_character(userid_owner)

    """ Test the None result (no failures), then manually clear the tags afterwards. """
    result = searchtag.associate(userid_tag_adder, tags, submitid=submitid)
    assert result is None
    result = searchtag.associate(userid_tag_adder, tags, journalid=journalid)
    assert result is None
    result = searchtag.associate(userid_tag_adder, tags, journalid=journalid)
    assert result is None
    searchtag.associate(userid_tag_adder, set(), submitid=submitid)
    searchtag.associate(userid_tag_adder, set(), journalid=journalid)
    searchtag.associate(userid_tag_adder, set(), journalid=journalid)

    """ Test the result:None variant (restricted tags added, no tags removed) """
    restricted_tag = searchtag.parse_restricted_tags("pearl")
    searchtag.edit_user_tag_restrictions(userid_owner, restricted_tag)
    result = searchtag.associate(userid_tag_adder, tags, submitid=submitid)
    assert "pearl" in result["add_failure_restricted_tags"]
    assert result["remove_failure_owner_set_tags"] is None
    result = searchtag.associate(userid_tag_adder, tags, charid=charid)
    assert "pearl" in result["add_failure_restricted_tags"]
    assert result["remove_failure_owner_set_tags"] is None
    result = searchtag.associate(userid_tag_adder, tags, journalid=journalid)
    assert "pearl" in result["add_failure_restricted_tags"]
    assert result["remove_failure_owner_set_tags"] is None
    searchtag.associate(userid_owner, set(), submitid=submitid)
    searchtag.associate(userid_owner, set(), charid=charid)
    searchtag.associate(userid_owner, set(), journalid=journalid)
    searchtag.edit_user_tag_restrictions(userid_owner, set())

    """Test the None:result variant (no restricted tags added, tag removal blocked)
    - Submission items will return None in this case (different method of preventing tag removal)
    - Character and journal items should return the None:result variant, as expected"""
    searchtag.associate(userid_owner, tags, submitid=submitid)
    searchtag.associate(userid_owner, tags, charid=charid)
    searchtag.associate(userid_owner, tags, journalid=journalid)
    result = searchtag.associate(userid_tag_adder, tags_two, submitid=submitid)
    assert result is None
    result = searchtag.associate(userid_tag_adder, tags_two, charid=charid)
    assert result["add_failure_restricted_tags"] is None
    assert "pearl" in result["remove_failure_owner_set_tags"]
    result = searchtag.associate(userid_tag_adder, tags_two, journalid=journalid)
    assert result["add_failure_restricted_tags"] is None
    assert "pearl" in result["remove_failure_owner_set_tags"]
    searchtag.associate(userid_owner, set(), submitid=submitid)
    searchtag.associate(userid_owner, set(), charid=charid)
    searchtag.associate(userid_owner, set(), journalid=journalid)

    """Test the result:result variant (restricted tags added, tag removal blocked)
    - Submission items will behave in the result:None variant
    - Character/Journal items will behave in the result:result manner"""
    restricted_tag = searchtag.parse_restricted_tags("profanity")
    searchtag.edit_user_tag_restrictions(userid_owner, restricted_tag)
    searchtag.associate(userid_owner, tags, submitid=submitid)
    searchtag.associate(userid_owner, tags, charid=charid)
    searchtag.associate(userid_owner, tags, journalid=journalid)
    # Effect upon adding this set: Remove user-set tag "pearl"; add restricted tag "profanity"
    tags_three = tags_two | {"profanity"}
    result = searchtag.associate(userid_tag_adder, tags_three, submitid=submitid)
    assert "profanity" in result["add_failure_restricted_tags"]
    assert result["remove_failure_owner_set_tags"] is None
    result = searchtag.associate(userid_tag_adder, tags_three, charid=charid)
    assert "profanity" in result["add_failure_restricted_tags"]
    assert "pearl" in result["remove_failure_owner_set_tags"]
    result = searchtag.associate(userid_tag_adder, tags_three, journalid=journalid)
    assert "profanity" in result["add_failure_restricted_tags"]
    assert "pearl" in result["remove_failure_owner_set_tags"]
Esempio n. 23
0
def control_tagrestrictions_post_(request):
    tags = searchtag.parse_restricted_tags(request.params["tags"])
    searchtag.edit_user_tag_restrictions(request.userid, tags)
    return Response(define.webpage(request.userid, "control/edit_tagrestrictions.html", (
        searchtag.query_user_restricted_tags(request.userid),
    ), title="Edit Community Tagging Restrictions"))
Esempio n. 24
0
def directorcontrol_globaltagrestrictions_post_(request):
    tags = searchtag.parse_restricted_tags(request.params["tags"])
    searchtag.edit_global_tag_restrictions(request.userid, tags)
    raise HTTPSeeOther(location="/directorcontrol/globaltagrestrictions")
Esempio n. 25
0
def directorcontrol_globaltagrestrictions_post_(request):
    tags = searchtag.parse_restricted_tags(request.params["tags"])
    searchtag.edit_global_tag_restrictions(request.userid, tags)
    raise HTTPSeeOther(location="/directorcontrol/globaltagrestrictions")
Esempio n. 26
0
def control_tagrestrictions_post_(request):
    tags = searchtag.parse_restricted_tags(request.params["tags"])
    searchtag.edit_user_tag_restrictions(request.userid, tags)
    return Response(define.webpage(request.userid, "control/edit_tagrestrictions.html", (
        searchtag.get_user_tag_restrictions(request.userid),
    )))
def test_tags_over_length_100_are_dropped():
    lengthy_tags = {"a" * 99, "a" * 100, "a" * 101}
    valid_with_lengthy = valid_tags | {"a" * 99, "a" * 100}

    assert valid_with_lengthy == searchtag.parse_restricted_tags(
        " ".join(valid_tags | lengthy_tags))
Esempio n. 28
0
def test_get_user_tag_restrictions():
    user_id = db_utils.create_user()
    tags = searchtag.parse_restricted_tags(", ".join(combined_tags))
    searchtag.edit_user_tag_restrictions(user_id, tags)
    resultant_tags = searchtag.get_user_tag_restrictions(user_id)
    assert resultant_tags == valid_tags
Esempio n. 29
0
def test_uppercase_tags_are_converted_to_lowercase():
    uppercase_tags = {'OMEGA_RUBY', 'ALPHA_SAPPHIRE', 'DIAMOND', 'PEARL', 'DIGI_*'}
    lowercase_tags = {'omega_ruby', 'alpha_sapphire', 'diamond', 'pearl', 'digi_*'}

    assert lowercase_tags == searchtag.parse_restricted_tags(" ".join(uppercase_tags))
Esempio n. 30
0
def test_get_user_tag_restrictions():
    user_id = db_utils.create_user()
    tags = searchtag.parse_restricted_tags(", ".join(combined_tags))
    searchtag.edit_user_tag_restrictions(user_id, tags)
    resultant_tags = searchtag.query_user_restricted_tags(user_id)
    assert resultant_tags == valid_tags
Esempio n. 31
0
def control_tagrestrictions_post_(request):
    tags = searchtag.parse_restricted_tags(request.params["tags"])
    searchtag.edit_user_tag_restrictions(request.userid, tags)

    raise HTTPSeeOther(location=request.route_path('control_tagrestrictions'))
Esempio n. 32
0
def test_tags_over_length_100_are_dropped():
    lengthy_tags = {"a" * 99, "a" * 100, "a" * 101}
    valid_with_lengthy = valid_tags | {"a" * 99, "a" * 100}

    assert valid_with_lengthy == searchtag.parse_restricted_tags(" ".join(valid_tags | lengthy_tags))