# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import unicode_literals

import pytest

import millionmilestogether.views.moments as views

from ..utils import (
    get_friend,
    rendered_template
)
from ..utils import make_ajax_request


@pytest.mark.django_db
def test_render_moment_thumbs_with_incorrect_login(rf, moment, other_user):
    request = rf.get(moment.get_thumbs_url())
    request = make_ajax_request(request)
    request.user = other_user
    kwargs = moment.get_url_kwargs()

    resp = views.moment_thumbs(request, **kwargs)
    assert resp.status_code == 403
    expected = b"You don't have permission to view this moment."
    assert resp.content == expected


@pytest.mark.django_db
def test_render_moment_thumbs_with_correct_login(rf, image):
    moment = image.moment
    request = rf.get(moment.get_thumbs_url())
    request = make_ajax_request(request)
    request.user = moment.owner
    kwargs = moment.get_url_kwargs()

    resp = views.moment_thumbs(request, **kwargs)
    assert resp.status_code == 200
    assert rendered_template(resp) == [
        'millionmilestogether/moment/_thumbs.html',
    ]


@pytest.mark.django_db
def test_render_moment_thumbs_without_xhr(rf, moment):
    request = rf.get(moment.get_thumbs_url())
    request.user = moment.owner
    kwargs = moment.get_url_kwargs()

    resp = views.moment_thumbs(request, **kwargs)
    assert resp.status_code == 403
    expected = b"This page is not directly accessible."
    assert resp.content == expected


@pytest.mark.django_db
def test_render_moment_thumbs_as_friend(rf, moment):
    friend = get_friend(moment.owner)
    request = rf.get(moment.get_thumbs_url())
    request = make_ajax_request(request)
    request.user = friend
    kwargs = moment.get_url_kwargs()

    resp = views.moment_thumbs(request, **kwargs)
    assert resp.status_code == 200
    assert rendered_template(resp) == [
        'millionmilestogether/moment/_thumbs.html',
    ]
