blob: 940116efa74e6eff372c0b8b5f007ce5d0ef4628 [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 project_views module."""
7from __future__ import print_function
8from __future__ import division
9from __future__ import absolute_import
10
11import unittest
12
13from framework import framework_views
14from project import project_views
15from proto import project_pb2
16from services import service_manager
17from testing import fake
18
19
20class ProjectAccessViewTest(unittest.TestCase):
21
22 def testAccessViews(self):
23 anyone_view = project_views.ProjectAccessView(
24 project_pb2.ProjectAccess.ANYONE)
25 self.assertEqual(anyone_view.key, int(project_pb2.ProjectAccess.ANYONE))
26
27 members_only_view = project_views.ProjectAccessView(
28 project_pb2.ProjectAccess.MEMBERS_ONLY)
29 self.assertEqual(members_only_view.key,
30 int(project_pb2.ProjectAccess.MEMBERS_ONLY))
31
32
33class ProjectViewTest(unittest.TestCase):
34
35 def setUp(self):
36 self.services = service_manager.Services(
37 project=fake.ProjectService(),
38 user=fake.UserService())
39 self.services.project.TestAddProject('test')
40
41 def testNormalProject(self):
42 project = self.services.project.GetProjectByName('fake cnxn', 'test')
43 project_view = project_views.ProjectView(project)
44 self.assertEqual('test', project_view.project_name)
45 self.assertEqual('/p/test', project_view.relative_home_url)
46 self.assertEqual('LIVE', project_view.state_name)
47
48 def testCachedContentTimestamp(self):
49 project = self.services.project.GetProjectByName('fake cnxn', 'test')
50
51 # Project was never updated since we added cached_content_timestamp.
52 project.cached_content_timestamp = 0
53 view = project_views.ProjectView(project, now=1 * 60 * 60 + 234)
54 self.assertEqual(1 * 60 * 60, view.cached_content_timestamp)
55
56 # Project was updated within the last hour, use that timestamp.
57 project.cached_content_timestamp = 1 * 60 * 60 + 123
58 view = project_views.ProjectView(project, now=1 * 60 * 60 + 234)
59 self.assertEqual(1 * 60 * 60 + 123, view.cached_content_timestamp)
60
61 # Project was not updated within the last hour, but user groups
62 # could have been updated on groups.google.com without any
63 # notification to us, so the client will ask for an updated feed
64 # at least once an hour.
65 project.cached_content_timestamp = 1 * 60 * 60 + 123
66 view = project_views.ProjectView(project, now=2 * 60 * 60 + 234)
67 self.assertEqual(2 * 60 * 60, view.cached_content_timestamp)
68
69
70class MemberViewTest(unittest.TestCase):
71
72 def setUp(self):
73 self.alice_view = framework_views.StuffUserView(111, 'alice', True)
74 self.bob_view = framework_views.StuffUserView(222, 'bob', True)
75 self.carol_view = framework_views.StuffUserView(333, 'carol', True)
76
77 self.project = project_pb2.Project()
78 self.project.project_name = 'proj'
79 self.project.owner_ids.append(111)
80 self.project.committer_ids.append(222)
81 self.project.contributor_ids.append(333)
82
83 def testViewingSelf(self):
84 member_view = project_views.MemberView(
85 0, 111, self.alice_view, self.project, None)
86 self.assertFalse(member_view.viewing_self)
87 member_view = project_views.MemberView(
88 222, 111, self.alice_view, self.project, None)
89 self.assertFalse(member_view.viewing_self)
90
91 member_view = project_views.MemberView(
92 111, 111, self.alice_view, self.project, None)
93 self.assertTrue(member_view.viewing_self)
94
95 def testRoles(self):
96 member_view = project_views.MemberView(
97 0, 111, self.alice_view, self.project, None)
98 self.assertEqual('Owner', member_view.role)
99 self.assertEqual('/p/proj/people/detail?u=111',
100 member_view.detail_url)
101
102 member_view = project_views.MemberView(
103 0, 222, self.bob_view, self.project, None)
104 self.assertEqual('Committer', member_view.role)
105 self.assertEqual('/p/proj/people/detail?u=222',
106 member_view.detail_url)
107
108 member_view = project_views.MemberView(
109 0, 333, self.carol_view, self.project, None)
110 self.assertEqual('Contributor', member_view.role)
111 self.assertEqual('/p/proj/people/detail?u=333',
112 member_view.detail_url)