blob: 92369bad409b0d0f0bf1af1809b0291bdbcca547 [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001# Copyright 2016 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style
3# license that can be found in the LICENSE file or at
4# https://developers.google.com/open-source/licenses/bsd
5
6"""Unit tests for hotlist_views classes."""
7from __future__ import print_function
8from __future__ import division
9from __future__ import absolute_import
10
11import unittest
12
13from features import hotlist_views
14from framework import authdata
15from framework import framework_views
16from framework import permissions
17from services import service_manager
18from testing import fake
19from proto import user_pb2
20
21
22class MemberViewTest(unittest.TestCase):
23
24 def setUp(self):
25 self.hotlist = fake.Hotlist('hotlistName', 123,
26 hotlist_item_fields=[
27 (2, 0, None, None, ''),
28 (1, 0, None, None, ''),
29 (5, 0, None, None, '')],
30 is_private=False, owner_ids=[111])
31 self.user1 = user_pb2.User(user_id=111)
32 self.user1_view = framework_views.UserView(self.user1)
33
34 def testMemberViewCorrect(self):
35 member_view = hotlist_views.MemberView(111, 111, self.user1_view,
36 self.hotlist)
37 self.assertEqual(member_view.user, self.user1_view)
38 self.assertEqual(member_view.detail_url, '/u/111/')
39 self.assertEqual(member_view.role, 'Owner')
40 self.assertTrue(member_view.viewing_self)
41
42
43class HotlistViewTest(unittest.TestCase):
44
45 def setUp(self):
46 self.services = service_manager.Services(user=fake.UserService(),
47 usergroup=fake.UserGroupService())
48 self.user1 = self.services.user.TestAddUser('user1', 111)
49 self.user1.obscure_email = True
50 self.user1_view = framework_views.UserView(self.user1)
51 self.user2 = self.services.user.TestAddUser('user2', 222)
52 self.user2.obscure_email = False
53 self.user2_view = framework_views.UserView(self.user2)
54 self.user3 = self.services.user.TestAddUser('user3', 333)
55 self.user3_view = framework_views.UserView(self.user3)
56 self.user4 = self.services.user.TestAddUser('user4', 444, banned=True)
57 self.user4_view = framework_views.UserView(self.user4)
58
59 self.user_auth = authdata.AuthData.FromEmail(
60 None, 'user3', self.services)
61 self.user_auth.effective_ids = {3}
62 self.user_auth.user_id = 3
63 self.users_by_id = {1: self.user1_view, 2: self.user2_view,
64 3: self.user3_view, 4: self.user4_view}
65 self.perms = permissions.EMPTY_PERMISSIONSET
66
67 def testNoOwner(self):
68 hotlist = fake.Hotlist('unowned', 500, owner_ids=[])
69 view = hotlist_views.HotlistView(hotlist, self.perms,
70 self.user_auth, 1, self.users_by_id)
71 self.assertFalse(view.url)
72
73 def testBanned(self):
74 # With a banned user
75 hotlist = fake.Hotlist('userBanned', 423, owner_ids=[4])
76 hotlist_view = hotlist_views.HotlistView(
77 hotlist, self.perms, self.user_auth, 1, self.users_by_id)
78 self.assertFalse(hotlist_view.visible)
79
80 # With a user not banned
81 hotlist = fake.Hotlist('userNotBanned', 453, owner_ids=[1])
82 hotlist_view = hotlist_views.HotlistView(
83 hotlist, self.perms, self.user_auth, 1, self.users_by_id)
84 self.assertTrue(hotlist_view.visible)
85
86 def testNoPermissions(self):
87 hotlist = fake.Hotlist(
88 'private', 333, is_private=True, owner_ids=[1], editor_ids=[2])
89 hotlist_view = hotlist_views.HotlistView(
90 hotlist, self.perms, self.user_auth, 1, self.users_by_id)
91 self.assertFalse(hotlist_view.visible)
92 self.assertEqual(hotlist_view.url, '/u/1/hotlists/private')
93
94 def testFriendlyURL(self):
95 # owner with obscure_email:false
96 hotlist = fake.Hotlist(
97 'noObscureHotlist', 133, owner_ids=[2], editor_ids=[3])
98 hotlist_view = hotlist_views.HotlistView(
99 hotlist, self.perms, self.user_auth,
100 viewed_user_id=3, users_by_id=self.users_by_id)
101 self.assertEqual(hotlist_view.url, '/u/user2/hotlists/noObscureHotlist')
102
103 #owner with obscure_email:true
104 hotlist = fake.Hotlist('ObscureHotlist', 133, owner_ids=[1], editor_ids=[3])
105 hotlist_view = hotlist_views.HotlistView(
106 hotlist, self.perms, self.user_auth, viewed_user_id=1,
107 users_by_id=self.users_by_id)
108 self.assertEqual(hotlist_view.url, '/u/1/hotlists/ObscureHotlist')
109
110 def testOtherAttributes(self):
111 hotlist = fake.Hotlist(
112 'hotlistName', 123, hotlist_item_fields=[(2, 0, None, None, ''),
113 (1, 0, None, None, ''),
114 (5, 0, None, None, '')],
115 is_private=False, owner_ids=[1],
116 editor_ids=[2, 3])
117 hotlist_view = hotlist_views.HotlistView(
118 hotlist, self.perms, self.user_auth, viewed_user_id=2,
119 users_by_id=self.users_by_id, is_starred=True)
120 self.assertTrue(hotlist_view.visible, True)
121 self.assertEqual(hotlist_view.role_name, 'editor')
122 self.assertEqual(hotlist_view.owners, [self.user1_view])
123 self.assertEqual(hotlist_view.editors, [self.user2_view, self.user3_view])
124 self.assertEqual(hotlist_view.num_issues, 3)
125 self.assertTrue(hotlist_view.is_starred)