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