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