# -*- coding: utf-8 -*-
import unittest

from nose.tools import *  # noqa (PEP8 asserts)
from flask import Flask
from webtest_plus import TestApp

from framework.exceptions import HTTPError
from framework.routing import json_renderer, process_rules, Rule

def error_view():
    raise HTTPError(400)

def error_with_msg():
    raise HTTPError(400, data={
        'message_short': 'Invalid',
        'message_long': 'Invalid request'
    })

class TestJSONRenderer(unittest.TestCase):

    def setUp(self):
        self.app = Flask(__name__)
        self.app.debug = True

        self.wt = TestApp(self.app)

    def test_error_handling(self):
        rule = Rule(['/error/'], 'get', error_view, renderer=json_renderer)
        process_rules(self.app, [rule])
        res = self.wt.get('/error/', expect_errors=True)
        assert_equal(res.status_code, 400)
        assert_true(isinstance(res.json, dict))

    def test_error_handling_with_message(self):
        rule = Rule(['/error/'], 'get', error_with_msg, renderer=json_renderer)
        process_rules(self.app, [rule])
        res = self.wt.get('/error/', expect_errors=True)
        assert_equal(res.status_code, 400)
        data = res.json
        assert_equal(data['message_short'], 'Invalid')
        assert_equal(data['message_long'], 'Invalid request')

@@ -249,16 +249,137 @@
 , Rule%0A%0A
+from tests import factories%0Afrom tests.base import OsfTestCase%0A%0Afrom website import routes%0Afrom website import settings%0A%0A
 def erro
@@ -1431,8 +1431,1259 @@
 quest')%0A
+%0A%0Aclass TestGetGlobals(OsfTestCase):%0A%0A    def setUp(self):%0A        super(TestGetGlobals, self).setUp()%0A%0A        self.inst_one = factories.InstitutionFactory()%0A        self.inst_two = factories.InstitutionFactory()%0A%0A        self.user = factories.AuthUserFactory()%0A        self.user.affiliated_institutions.append(self.inst_one)%0A        self.user.affiliated_institutions.append(self.inst_two)%0A        self.user.save()%0A%0A        for i in range(settings.INSTITUTION_DISPLAY_NODE_THRESHOLD):%0A            node = factories.NodeFactory(creator=self.user, is_public=True)%0A            node.affiliated_institutions.append(self.inst_one)%0A            node.save()%0A%0A        for i in range(settings.INSTITUTION_DISPLAY_NODE_THRESHOLD - 1):%0A            node = factories.NodeFactory(creator=self.user, is_public=True)%0A            node.affiliated_institutions.append(self.inst_two)%0A            node.save()%0A%0A    def test_get_globals_dashboard_institutions(self):%0A        globals = routes.get_globals()%0A        dashboard_institutions = globals%5B'dashboard_institutions'%5D%0A        assert_equal(len(dashboard_institutions), 1)%0A        assert_equal(dashboard_institutions%5B0%5D%5B'id'%5D, self.inst_one._id)%0A        assert_not_equal(dashboard_institutions%5B0%5D%5B'id'%5D, self.inst_two._id)%0A

Add test for getting dashboard institutions.