# -*- coding: utf-8 -*-
import os
import Image, StringIO
import allura

from nose.tools import assert_true

from ming.orm.ormsession import ThreadLocalORMSession

from allura import model as M
from allura.lib import helpers as h
from allura.tests import decorators as td
from alluratest.controller import TestController

from forgewiki import model

#---------x---------x---------x---------x---------x---------x---------x
# RootController methods exposed:
#     index, new_page, search
# PageController methods exposed:
#     index, edit, history, diff, raw, revert, update
# CommentController methods exposed:
#     reply, delete

class TestRootController(TestController):
    def setUp(self):
        super(TestRootController, self).setUp()
        self.setup_with_tools()

    @td.with_wiki
    def setup_with_tools(self):
        pass

    def test_root_index(self):
        r = self.app.get('/wiki/tést/').follow()
        assert 'tést' in r
        assert 'Create Page' in r
        # No 'Create Page' button if user doesn't have 'create' perm
        r = self.app.get('/wiki/tést/',
                extra_environ=dict(username='*anonymous')).follow()
        assert 'Create Page' not in r

    def test_root_markdown_syntax(self):
        response = self.app.get('/wiki/markdown_syntax/')
        assert 'Markdown Syntax' in response

    def test_root_browse_tags(self):
        response = self.app.get('/wiki/browse_tags/')
        assert 'Browse Labels' in response

    def test_root_browse_pages(self):
        response = self.app.get('/wiki/browse_pages/')
        assert 'Browse Pages' in response

    def test_root_new_page(self):
        response = self.app.get('/wiki/new_page?title=tést')
        assert 'tést' in response

    def test_root_new_search(self):
        self.app.get('/wiki/tést/')
        response = self.app.get('/wiki/search?q=tést')
        assert 'Search wiki: tést' in response

    def test_page_index(self):
        response = self.app.get('/wiki/tést/')
        assert 'tést' in response.follow()

    def test_page_edit(self):
        self.app.get('/wiki/tést/index')
        response = self.app.post('/wiki/tést/edit')
        assert 'tést' in response

    def test_title_slashes(self):
        # forward slash not allowed in wiki page title - converted to dash
        response = self.app.post(
            '/wiki/foo-bar/update',
            params={
                'title':'foo/bar',
                'text':'sometext',
                'labels':'',
                'labels_old':'',
                'viewable_by-0.id':'all'}).follow()
        assert 'foo-bar' in response
        assert 'foo-bar' in response.request.url

    def test_dotted_page_name(self):
        r = self.app.post(
            '/wiki/page.dot/update',
            params={
                'title':'page.dot',
                'text':'text1',
                'labels':'',
                'labels_old':'',
                'viewable_by-0.id':'all'}).follow()
        assert 'page.dot' in r

    def test_subpage_attempt(self):
        self.app.get('/wiki/tést/')
        self.app.post(
            '/wiki/tést/update',
            params={
                'title':'tést',
                'text':'text1',
                'labels':'',
                'labels_old':'',
                'viewable_by-0.id':'all'})
        assert '/p/test/wiki/Home/' in self.app.get('/wiki/tést/Home/')
        self.app.get('/wiki/tést/notthere/', status=404)

    def test_page_history(self):
        self.app.get('/wiki/tést/')
        self.app.post(
            '/wiki/tést/update',
            params={
                'title':'tést',
                'text':'text1',
                'labels':'',
                'labels_old':'',
                'viewable_by-0.id':'all'})
        self.app.post(
            '/wiki/tést/update',
            params={
                'title':'tést',
                'text':'text2',
                'labels':'',
                'labels_old':'',
                'viewable_by-0.id':'all'})
        response = self.app.get('/wiki/tést/history')
        assert 'tést' in response
        # two revisions are shown
        assert '2 by Test Admin' in response
        assert '1 by Test Admin' in response
        # you can revert to an old revison, but not the current one
        assert response.html.find('a',{'href':'./revert?version=1'})
        assert not response.html.find('a',{'href':'./revert?version=2'})
        response = self.app.get('/wiki/tést/history', extra_environ=dict(username='*anonymous'))
        # two revisions are shown
        assert '2 by Test Admin' in response
        assert '1 by Test Admin' in response
        # you cannot revert to any revision
        assert not response.html.find('a',{'href':'./revert?version=1'})
        assert not response.html.find('a',{'href':'./revert?version=2'})

    def test_page_diff(self):
        self.app.post(
            '/wiki/tést/update',
            params={
                'title':'tést',
                'text':'sometext',
                'labels':'',
                'labels_old':'',
                'viewable_by-0.id':'all'})
        self.app.post('/wiki/tést/revert', params=dict(version='1'))
        response = self.app.get('/wiki/tést/')
        assert 'Subscribe' in response
        response = self.app.get('/wiki/tést/diff?v1=0&v2=0')
        assert 'tést' in response

    def test_page_raw(self):
        self.app.post(
            '/wiki/TEST/update',
            params={
                'title':'TEST',
                'text':'sometext',
                'labels':'',
                'labels_old':'',
                'viewable_by-0.id':'all'})
        response = self.app.get('/wiki/TEST/raw')
        assert 'TEST' in response

    def test_page_revert_no_text(self):
        self.app.post(
            '/wiki/tést/update',
            params={
                'title':'tést',
                'text':'',
                'labels':'',
                'labels_old':'',
                'viewable_by-0.id':'all'})
        response = self.app.post('/wiki/tést/revert', params=dict(version='1'))
        assert '.' in response.json['location']
        response = self.app.get('/wiki/tést/')
        assert 'tést' in response

    def test_page_revert_with_text(self):
        self.app.get('/wiki/tést/')
        self.app.post(
            '/wiki/tést/update',
            params={
                'title':'tést',
                'text':'sometext',
                'labels':'',
                'labels_old':'',
                'viewable_by-0.id':'all'})
        response = self.app.post('/wiki/tést/revert', params=dict(version='1'))
  
      assert '.' in response.json['location']
        response = self.app.get('/wiki/tést/')
        assert 'tést' in response

    def test_page_update(self):
        self.app.get('/wiki/tést/')
        response = self.app.post(
   
         '/wiki/tést/update',
            params={
                'title':'tést',
                'text':'sometext',
                'labels':'',
                'labels_old':'',
                'viewable_by-0.id':'all'})
        assert 'tést' in response

    def test_page_label_unlabel(self):
        self.app.get('/wiki/tést/')
        response = self.app.post(
            '/wiki/tést/update',
            params={
                'title':'tést',
                'text':'sometext',
                'labels':'yellow,green',
                'labels_old':'yellow,green',
                'viewable_by-0.id':'all'})
        assert 'tést' in response
        response = self.app.post(
            '/wiki/tést/update',
            params={
                'title':'tést',
                'text':'sometext',
                'labels':'yellow',
                'labels_old':'yellow',
                'viewable_by-0.id':'all'})
        assert 'tést' in response

    def test_new_attachment(self):
        self.app.post(
            '/wiki/tést/update',
            params={
                'title':'tést',
                'text':'sometext',
                'labels':'',
                'labels_old':'',
                'viewable_by-0.id':'all'})
        content = file(__file__).read()
        self.app.post('/