コード例 #1
0
  def testGetAccessibleLayers(self):
    self.mox.StubOutWithMock(model, 'Layer')
    mock_layers = [self.mox.CreateMockAnything() for _ in xrange(3)]
    dummy_user = object()
    permission = model.Permission

    model.Layer.all().AndReturn(mock_layers)
    mock_layers[0].IsPermitted(dummy_user, permission.ACCESS).AndReturn(False)
    mock_layers[1].IsPermitted(dummy_user, permission.ACCESS).AndReturn(True)
    mock_layers[1].IsPermitted(dummy_user, permission.MANAGE).AndReturn(False)
    mock_layers[2].IsPermitted(dummy_user, permission.ACCESS).AndReturn(True)
    mock_layers[2].IsPermitted(dummy_user, permission.MANAGE).AndReturn(True)

    self.mox.ReplayAll()
    self.assertEqual(base._GetAccessibleLayers(dummy_user), mock_layers[1:])
    self.assertEqual(mock_layers[1].managed, False)
    self.assertEqual(mock_layers[2].managed, True)
コード例 #2
0
  def testRenderTemplate(self):
    self.mox.StubOutWithMock(os.path, 'dirname')
    self.mox.StubOutWithMock(base, '_GetAccessibleLayers')
    mock_layer = self.mox.CreateMockAnything()
    mock_layer.permission_set = self.mox.CreateMockAnything()
    mock_user = self.mox.CreateMockAnything()
    self.mox.StubOutWithMock(users, 'get_current_user')
    self.mox.StubOutWithMock(users, 'create_logout_url')
    self.mox.StubOutWithMock(users, 'is_current_user_admin')
    self.mox.StubOutWithMock(template, 'register_template_library')
    self.mox.StubOutWithMock(template, 'render')

    handler = base.PageHandler()
    handler.response = self.mox.CreateMockAnything()
    handler.response.out = self.mox.CreateMockAnything()
    mock_permissions = [self.mox.CreateMockAnything() for _ in xrange(2)]
    mock_permissions[0].type = 'a'
    mock_permissions[1].type = 'b'
    dummy_category = object()
    dummy_accessible_layers = object()
    dummy_logout_url = object()
    dummy_nickname = object()
    dummy_is_admin = object()
    dummy_result = object()
    name = 'dummy_name'
    args = {'x': 'y', 'z': 'w'}

    @mox.Func
    def VerifyArgs(args):
      expected_args = ['logout_url', 'username', 'is_admin', 'debug', 'layer',
                       'active_permissions', 'all_layers', 'category',
                       'x', 'z']
      self.assertEqual(set(args), set(expected_args))
      self.assertEqual(args['logout_url'], dummy_logout_url)
      self.assertEqual(args['username'], dummy_nickname)
      self.assertEqual(args['is_admin'], dummy_is_admin)
      self.assertEqual(args['debug'], True)
      self.assertEqual(args['layer'], mock_layer)
      self.assertEqual(set(args['active_permissions']), set(['a', 'b']))
      self.assertTrue(args['active_permissions']['a'])
      self.assertTrue(args['active_permissions']['b'])
      self.assertEqual(args['all_layers'], dummy_accessible_layers)
      self.assertEqual(args['category'], {dummy_category: True})
      return True

    users.get_current_user().AndReturn(mock_user)
    base._GetAccessibleLayers(mock_user).AndReturn(dummy_accessible_layers)
    mock_layer.permission_set.filter('user', mock_user).AndReturn(
        mock_permissions)
    users.create_logout_url('/').AndReturn(dummy_logout_url)
    mock_user.nickname().AndReturn(dummy_nickname)
    users.is_current_user_admin().AndReturn(dummy_is_admin)
    template.register_template_library('template_functions.pages')
    os.path.dirname(mox.IgnoreArg()).AndReturn('dummy_dirname')
    template.render('dummy_dirname/../html_templates/dummy_name.html',
                    VerifyArgs).AndReturn(dummy_result)
    handler.response.out.write(dummy_result)

    self.mox.ReplayAll()

    handler.RenderTemplate(mock_layer, dummy_category, name, args)