# 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, 9, 29, 29), 'rbac.common.logs.get_logger', 'get_logger', ({(29, 20, 29, 28): '__name__'}, {}), '(__name__)', False, 'from rbac.common.logs import get_logger\n'), ((38, 11, 38, 49), 'tests.rbac.helper.api.role.create.new', 'helper.api.role.create.new', (), '', False, 'from tests.rbac import helper\n'), ((40, 10, 40, 64), 'tests.rbac.helper.api.role.member.propose.url', 'helper.api.role.member.propose.url', (), '', False, 'from tests.rbac import helper\n'), ((42, 11, 42, 60), '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, 15, 45, 5), 'requests.post', 'requests.post', (), '', False, 'import requests\n'), ((46, 13, 46, 41), 'tests.rbac.api.assertions.assert_api_success', 'assert_api_success', ({(46, 32, 46, 40): 'response'}, {}), '(response)', False, 'from tests.rbac.api.assertions import assert_api_success\n'), ((48, 4, 48, 19), 'time.sleep', 'time.sleep', ({(48, 15, 48, 18): '(0.5)'}, {}), '(0.5)', False, 'import time\n'), ((49, 15, 49, 68), 'tests.rbac.helper.api.proposal.get', 'helper.api.proposal.get', ({(49, 39, 49, 60): "result['proposal_id']", (49, 62, 49, 67): 'owner'}, {}), "(result['proposal_id'], owner)", False, 'from tests.rbac import helper\n'), ((65, 10, 65, 64), 'tests.rbac.helper.api.role.member.propose.url', 'helper.api.role.member.propose.url', (), '', False, 'from tests.rbac import helper\n'), ((67, 15, 69, 5), 'requests.post', 'requests.post', (), '', False, 'import requests\n'), ((70, 4, 70, 72), 'tests.rbac.api.assertions.assert_api_error', 'assert_api_error', ({(70, 21, 70, 29): 'response', (70, 31, 70, 66): '"""Bad Request: id field is required"""', (70, 68, 70, 71): '(400)'}, {}), "(response, 'Bad Request: id field is required', 400)", False, 'from tests.rbac.api.assertions import assert_api_error\n')]