示例#1
0
def admin_user_remove():
    """
    Function admin remove_user route
    """
    input_data = request.get_json()
    admin.remove_user(input_data['token'], int(input_data['u_id']))
    return dumps({})
def test_remove_user_nvt():
    """ Test if AccessError raised if token not valid. """

    user = auth.auth_register("*****@*****.**", "password", "Meh", "Emoji")

    with pytest.raises(error.AccessError):
        admin.remove_user("invalidtoken", user['u_id'])
def test_remove_user_invalid_target():
    """ Test if InputError raised if u_id does not refer to a valid user. """

    user = auth.auth_register("*****@*****.**", "password", "Ru", "Paul")

    with pytest.raises(error.InputError):
        admin.remove_user(user['token'], -100)
def test_remove_user_not_owner():
    """ Test if AccessError raised if user not an owner of the Slackr. """

    user1 = auth.auth_register("*****@*****.**", "password", "Pepe", "Silvia")
    user2 = auth.auth_register("*****@*****.**", "password", "Ongo", "Gablogian")

    with pytest.raises(error.AccessError):
        admin.remove_user(user2['token'], user1['u_id'])
def main():

    print("\n" + INDENT + "Hello and welcome to 'Carpentry Shop' admin!\n")

    print("Menu:\n-----\n")

    choice = menu()

    while choice != EXIT:

        if choice == REG_USR:
            username = input("Enter username: "******"Enter Password: "******"Enter username: "******"Enter username: "******"The user exists!")
            else:
                print("No such user!")

        elif choice == DEL_ALL_USRS:
            admin.clear_users()
            print("All users cleared from the system successfully!")

        elif choice == ADD_ITEM:
            username = input("Enter username: "******"Enter the item's name: ")
            price = input("Enter the item's price: ")

            admin.add_item(username, item, price)

        elif choice == GEN_JSON_ARR:

            filename = input("Name your file: ")

            password_hasher.create_json_list(filename)

        elif choice == INPUT_JSON_ARR:

            filename = input("Enter full file name: ")

            admin.register_json_list(filename)

        choice = menu()

    return 0
    def test_remove(self):
        user_to_reg = "x"

        db.reference().child('users').child(user_to_reg).child('username').set(
            user_to_reg)
        db.reference().child('users').child(user_to_reg).child('password').set(
            java_hashcode("1234"))

        admin.remove_user(user_to_reg)
        user_to_check = db.reference().child('users').child(user_to_reg).get()
        self.assertIsNone(user_to_check)
示例#7
0
def remove_admin(request, username):
    result = Result()
    try:
        remove_user(username)
        result.code = Consts.SUCCESS_CODE
    except User.DoesNotExist:
        result.code = Consts.FAILED_CODE
        result.msg = Consts.NOT_FOUND_USER_MSG
    except:
        result.code = Consts.FAILED_CODE
        result.msg = Consts.UNKNOWN_ERROR
    return HttpResponse(json.dumps(result.to_dict()), content_type="application/json")
def test_remove_user():
    """
    Tests if user_remove correctly removes a user from the required parts of database:

    -retained in USERS, PASSWORD_DATA_LIST and in CHANNEL_DATA_LIST (for messages)
    -permission_id of user is now terminated (66)
    -removed from:
        CURRENT_USERS,
        CHANNELS[owner_id]/[member_ID],
    """

    # establish a test register
    test_dict = auth.auth_register("*****@*****.**", "password", "Bob", "Ross")

    # valid channel ID (assigned by channels_create)
    c_id_dict = channels.channels_create(test_dict["token"], "test rum ham", True)

    # create a second user and add them to the channel as an owner
    test_dict_2 = auth.auth_register("*****@*****.**", "password2", "James", "May")
    channel.channel_join(test_dict_2["token"], c_id_dict["channel_id"])
    channel.channel_addowner(test_dict["token"], c_id_dict["channel_id"], test_dict_2["u_id"])

    # store our details before the leave
    details_before = channel.channel_details(test_dict["token"], c_id_dict["channel_id"])
    # ensure there are two members: test_dict & test_dict_2
    assert len(details_before["all_members"]) == 2
    assert details_before["all_members"][0]["u_id"] == test_dict["u_id"]
    assert details_before["all_members"][1]["u_id"] == test_dict_2["u_id"]
    #ensure there are two owners: test_dict & test_dict_2
    assert len(details_before["owner_members"]) == 2
    assert details_before["owner_members"][0]["u_id"] == test_dict["u_id"]
    assert details_before["owner_members"][1]["u_id"] == test_dict_2["u_id"]

    # now we send a message to test rum ham
    message.message_send(test_dict_2["token"], c_id_dict["channel_id"], "hello world")
    # test that messages successfully returns the message
    sent_messages = channel.channel_messages(test_dict["token"], c_id_dict["channel_id"], 0)
    assert sent_messages["messages"][0]["message"] == "hello world"

    ######## Now we call the user_remove function:
    admin.remove_user(test_dict["token"], test_dict_2["u_id"])
    ########

    # we test that they still remain in user
    all_users = database.get_users()
    assert len(all_users) == 2
    assert all_users[0]["u_id"] == test_dict["u_id"]
    assert all_users[1]["u_id"] == test_dict_2["u_id"]

    # we test that their messages can still be printed
    messages_post_remove = channel.channel_messages(test_dict["token"], c_id_dict["channel_id"], 0)
    assert messages_post_remove["messages"][0]["message"] == "hello world"
    assert messages_post_remove["messages"][0]["u_id"] == test_dict_2["u_id"]

    # we test that their permission is changed to 66
    terminated_id = 66
    permissions_post_remove = database.get_permission_dict(test_dict_2["u_id"])
    assert permissions_post_remove["u_id"] == test_dict_2["u_id"]
    assert permissions_post_remove["permission_id"] == terminated_id

    # we test that they are no longer a part of CURRENT_USERS,
    assert database.get_current_user(test_dict_2["token"]) is None

    # we test that they are no longer a part of CHANNELS[owner_id]/[member_ID],
    details_after = channel.channel_details(test_dict["token"], c_id_dict["channel_id"])
    # ensure there is one member: test_dict
    assert len(details_after["all_members"]) == 1
    assert details_after["all_members"][0]["u_id"] == test_dict["u_id"]
    # ensure there is one owner: test_dict
    assert len(details_after["owner_members"]) == 1
    assert details_after["owner_members"][0]["u_id"] == test_dict["u_id"]

    # we test that they are unable to log in again
    password_test = "qwertyuiop"
    with pytest.raises(error.AccessError):
        auth.auth_login("*****@*****.**", "password2")

    # Test that the InputError raised when an unregistered email is entered
    # still takes priority over the AccessError of admin_user_remove.
    with pytest.raises(error.InputError):
        auth.auth_login("*****@*****.**", password_test)

    # Check incorrect password works stil
    with pytest.raises(error.AccessError):
        auth.auth_login("*****@*****.**", password_test)

    with pytest.raises(error.InputError):
        auth.auth_login("wrongemailformat", password_test)