# Copyright 2018 Contributors to Hyperledger Sawtooth
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# -----------------------------------------------------------------------------
""" Propose Role Add Member Test """
# pylint: disable=invalid-name

import time
import requests
import pytest

from rbac.common.logs import get_logger

from tests.rbac import helper
from tests.rbac.api.assertions import assert_api_error
from tests.rbac.api.assertions import assert_api_success
from tests.rbac.api.assertions import assert_api_post_requires_auth

LOGGER = get_logger(__name__)


@pytest.mark.api
@pytest.mark.api_role
def test_api_propose_role_member():
    """ Test a user proposing to add themselves to a role
    """
    owner = helper.api.user.current
    role = helper.api.role.create.new(user=owner)
    user = helper.api.user.current2
    url = helper.api.role.member.propose.url(role_id=role["id"])
    data = {"id": user["user_id"]}
    assert assert_api_post_requires_auth(url=url, json=data)
    response = requests.post(
        url=url, headers={"Authorization": user["token"]}, json=data
    )
    result = assert_api_success(response)
    assert result["proposal_id"]
    time.sleep(0.5)  # temporary until API refactored to return the proposal
    proposal = helper.api.proposal.get(result["proposal_id"], owner)
    assert proposal["id"] == result["proposal_id"]
    assert proposal["status"] == "OPEN"
    assert proposal["type"] == "ADD_ROLE_MEMBER"
    assert proposal["object"] == role["id"]
    assert proposal["target"] == user["user_id"]
    assert proposal["opener"] == user["user_id"]


@pytest.mark.api
@pytest.mark.api_role
def test_api_propose_role_member_required_fields():
    """ Test proposing adding a member to a role with missing fields
    """
    role, _ = helper.api.role.current
    user = helper.api.user.create.current
    url = helper.api.role.member.propose.url(role_id=role["id"])
    data = {}
    response = requests.post(
        url=url, headers={"Authorization": user["token"]}, json=data
    )
    assert_api_error(response, "Bad Request: id field is required", 400)

[(29, 'rbac.common.logs.get_logger', 'get_logger', False, 'from rbac.common.logs import get_logger\n'), (38, 'tests.rbac.helper.api.role.create.new', 'helper.api.role.create.new', False, 'from tests.rbac import helper\n'), (40, 'tests.rbac.helper.api.role.member.propose.url', 'helper.api.role.member.propose.url', False, 'from tests.rbac import helper\n'), (42, 'tests.rbac.api.assertions.assert_api_post_requires_auth', 'assert_api_post_requires_auth', False, 'from tests.rbac.api.assertions import assert_api_post_requires_auth\n'), (43, 'requests.post', 'requests.post', False, 'import requests\n'), (46, 'tests.rbac.api.assertions.assert_api_success', 'assert_api_success', False, 'from tests.rbac.api.assertions import assert_api_success\n'), (48, 'time.sleep', 'time.sleep', False, 'import time\n'), (49, 'tests.rbac.helper.api.proposal.get', 'helper.api.proposal.get', False, 'from tests.rbac import helper\n'), (65, 'tests.rbac.helper.api.role.member.propose.url', 'helper.api.role.member.propose.url', False, 'from tests.rbac import helper\n'), (67, 'requests.post', 'requests.post', False, 'import requests\n'), (70, 'tests.rbac.api.assertions.assert_api_error', 'assert_api_error', False, 'from tests.rbac.api.assertions import assert_api_error\n')]