def test_edit_own_password_admin(self, test_client):
        response = test_client.post(
            "/users/edit",
            headers={"apiKey": admin().api_key},
            data=json.dumps({"password": standard_user().password + "2"}))
        assert response.status_code == 200

        response = test_client.post("/authenticate",
                                    data=json.dumps({
                                        "username":
                                        admin().username,
                                        "password":
                                        standard_user().password + "2"
                                    }))
        assert response.status_code == 200
def test_config(tmpdir_factory, session):
    session.add(admin(True))
    session.add(standard_user(True))
    session.commit()

    root_directory = tmpdir_factory.mktemp('data')
    app.config['DATA_DIRECTORY'] = str(root_directory)
 def test_register_successful(self, test_client, test_user):
     response = test_client.post("/users/register",
                                 headers={"apiKey": admin().api_key},
                                 data=json.dumps(test_user),
                                 follow_redirects=True)
     assert response.status_code == 204
     assert os.path.exists(
         os.path.join(app.config['DATA_DIRECTORY'], test_user["username"]))
 def test_register_invalid_api_key(self, test_client, test_user):
     response = test_client.post(
         "/users/register",
         headers={"apiKey": "NOT_{}".format(admin().api_key)},
         data=json.dumps(test_user),
         follow_redirects=True)
     error = ErrorCodeAndMessageSchema().load(load_json_data(response)).data
     assert error == INVALID_API_KEY
    def test_register_already_existing_username(self, test_client, test_user):
        response = test_client.post("/users/register",
                                    headers={"apiKey": admin().api_key},
                                    data=json.dumps(test_user),
                                    follow_redirects=True)
        assert response.status_code == 204

        response2 = test_client.post("/users/register",
                                     headers={"apiKey": admin().api_key},
                                     data=json.dumps(test_user),
                                     follow_redirects=True)
        error = ErrorCodeAndMessageSchema().load(
            load_json_data(response2)).data

        expected_error_code_and_message = copy.deepcopy(
            USERNAME_ALREADY_EXISTS)
        expected_error_code_and_message.error_message = expected_error_code_and_message.error_message.format(
            test_user["username"])
        assert error == expected_error_code_and_message
    def test_register_successful_and_login(self, test_client, test_user):
        response = test_client.post("/users/register",
                                    headers={"apiKey": admin().api_key},
                                    data=json.dumps(test_user),
                                    follow_redirects=True)
        assert response.status_code == 204

        response = test_client.post("/authenticate",
                                    data=json.dumps(test_user),
                                    follow_redirects=True)

        assert response.status_code == 200
 def test_edit_password_user_not_exist(self, test_client):
     response = test_client.post("/users/edit",
                                 headers={"apiKey": admin().api_key},
                                 data=json.dumps({
                                     "username":
                                     "******",
                                     "password":
                                     standard_user().password + "2"
                                 }))
     error = error_from_response(response)
     expected_error_code_and_message = copy.deepcopy(USER_DOES_NOT_EXIST)
     expected_error_code_and_message.error_message = expected_error_code_and_message.error_message.format(
         "does_not_exist")
     assert error == expected_error_code_and_message
    def test_register_already_existing_user_folder_overwrite_folder(
            self, test_client, test_user):
        user_folder_path = os.path.join(app.config['DATA_DIRECTORY'],
                                        test_user["username"])
        os.mkdir(user_folder_path)
        with open(os.path.join(user_folder_path, "file.json"), 'w') as f:
            f.write('{"test": "json"}')

        response = test_client.post("/users/register",
                                    headers={"apiKey": admin().api_key},
                                    data=json.dumps(test_user),
                                    follow_redirects=True)

        assert response.status_code == 204

        folder_final_content = os.listdir(user_folder_path)
        assert len(folder_final_content) == 1
        assert "executions" in folder_final_content
        assert "file.json" not in folder_final_content