# -*- coding: utf-8 -*-
import pytest
from framework.auth import Auth
from api.base.settings.defaults import API_BASE
from osf_tests.factories import (
    AuthUserFactory,
    ProjectFactory,
    PrivateLinkFactory,
)
from osf.models import NodeLog

@pytest.fixture()
def admin_contrib():
    return AuthUserFactory()

@pytest.fixture()
def write_contrib():
    return AuthUserFactory()

@pytest.fixture()
def read_contrib():
    return AuthUserFactory()

@pytest.fixture()
def project(admin_contrib, write_contrib, read_contrib):
    project = ProjectFactory(creator=admin_contrib)
    project.add_contributor(write_contrib, ['write', 'read'])
    project.add_contributor(read_contrib, ['read'])
    project.save()
    return project

@pytest.fixture()
def url(project):
    return '/{}nodes/{}/settings/'.format(API_BASE, project._id)


@pytest.mark.django_db
class TestNodeSettingsGet:

    @pytest.fixture()
    def non_contrib(self):
        return AuthUserFactory()

    def test_node_settings_detail(self, app, admin_contrib, non_contrib, write_contrib, url, project):

        # non logged in uers can't access node settings
        res = app.get(url, expect_errors=True)
        assert res.status_code == 401

        # non_contrib can't access node settings
        res = app.get(url, auth=non_contrib.auth, expect_errors=True)
        assert res.status_code == 403

        # read contrib can access node settings
        res = app.get(url, auth=write_contrib.auth)
        assert res.status_code == 200

        # admin can access node settings
        res = app.get(url, auth=admin_contrib.auth)
        assert res.status_code == 200

        # allow_access_requests
        project.allow_access_requests = True
        project.save()
        res = app.get(url, auth=admin_contrib.auth)
        attributes = res.json['data']['attributes']
        assert attributes['access_requests_enabled'] is True

        # anyone can comment
        project.comment_level = 'public'
        project.save()
        res = app.get(url, auth=admin_contrib.auth)
        attributes = res.json['data']['attributes']
        assert attributes['anyone_can_comment'] is True

        project.comment_level = 'private'
        project.save()
        res = app.get(url, auth=admin_contrib.auth)
        attributes = res.json['data']['attributes']
        assert attributes['anyone_can_comment'] is False

        # wiki enabled
        res = app.get(url, auth=admin_contrib.auth)
        attributes = res.json['data']['attributes']
        assert attributes['wiki_enabled'] is True

        project.delete_addon('wiki', auth=Auth(admin_contrib))
        project.save()
        res = app.get(url, auth=admin_contrib.auth)