tests/app/soc/views/model/test_base.py
author Sverre Rabbelier <srabbelier@gmail.com>
Sun, 15 Mar 2009 15:21:06 +0000
changeset 1871 091118f6e886
parent 1691 c1d5a67e9e33
child 2915 76b5ef0fb5c6
permissions -rw-r--r--
Replaced a '.' with '?' in org app Patch by: Sverre Rabbelier
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1691
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     2
#
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     3
# Copyright 2009 the Melange authors.
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     4
#
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     8
#
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    10
#
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    15
# limitations under the License.
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    16
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    17
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    18
__authors__ = [
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    19
  '"Sverre Rabbelier" <sverre@rabbelier.nl>',
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    20
  ]
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    21
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    22
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    23
import unittest
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    24
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    25
from tests.test_utils import MockRequest
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    26
from tests.pymox import stubout
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    27
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    28
from tests.app.soc.logic.models.test_model import TestModelLogic
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    29
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    30
from soc.views.helper import access
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    31
from soc.views.helper import responses
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    32
from soc.views.models import base
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    33
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    34
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    35
def error_raw(error, request, template=None, context=None):
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    36
  """
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    37
  """
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    38
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    39
  return {
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    40
      'error': error,
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    41
      'request': request,
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    42
      'template': template,
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    43
      'context': context,
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    44
      }
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    45
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    46
def respond_raw(request, template, context=None, args=None, headers=None):
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    47
  """
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    48
  """
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    49
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    50
  return {
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    51
      'request': request,
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    52
      'template': template,
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    53
      'context': context,
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    54
      'args': args,
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    55
      'headers': headers,
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    56
      }
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    57
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    58
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    59
class TestView(base.View):
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    60
  """
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    61
  """
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    62
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    63
  def __init__(self):
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    64
    """
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    65
    """
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    66
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    67
    rights = access.Checker(None)
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    68
    rights['unspecified'] = ['deny']
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    69
    rights['any_access'] = ['allow']
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    70
    rights['show'] = ['allow']
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    71
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    72
    params = {}
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    73
    params['name'] = "Test"
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    74
    params['logic'] = TestModelLogic()
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    75
    params['rights'] = rights
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    76
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    77
    super(TestView, self).__init__(params=params)
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    78
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    79
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    80
class BaseTest(unittest.TestCase):
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    81
  """Tests related to the base view.
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    82
  """
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    83
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    84
  def setUp(self):
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    85
    """Set up required for the view tests.
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    86
    """
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    87
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    88
    self.view = TestView()
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    89
    self.stubout = stubout.StubOutForTesting()
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    90
    self.stubout.Set(responses, 'respond', respond_raw)
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    91
    self.stubout.Set(responses, 'errorResponse', error_raw)
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    92
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    93
  def testErrorOnNonExistantEntity(self):
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    94
    """
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    95
    """
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    96
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    97
    request = MockRequest("/test/public")
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    98
    access_type = "show"
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    99
    page_name = "Show Test"
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   100
    django_args = {'link_id': 'foo', 'scope_path': 'bar'}
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   101
    actual = self.view.public(request, access_type, page_name=page_name, **django_args)
c1d5a67e9e33 Add a very basic view test
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   102
    self.assertTrue('error' in actual)