def testSetUserPrefs_Anon(self):
     """Anon cannot set prefs."""
     request = users_pb2.SetUserPrefsRequest()
     mc = monorailcontext.MonorailContext(self.services,
                                          cnxn=self.cnxn,
                                          requester=None)
     with self.assertRaises(exceptions.InputException):
         self.CallWrapped(self.users_svcr.SetUserPrefs, mc, request)
    def testSetUserPrefs_Mine_Empty(self):
        """Setting zero prefs is a no-op.."""
        request = users_pb2.SetUserPrefsRequest()
        mc = monorailcontext.MonorailContext(self.services,
                                             cnxn=self.cnxn,
                                             requester='*****@*****.**')
        self.CallWrapped(self.users_svcr.SetUserPrefs, mc, request)

        prefs_after = self.services.user.GetUserPrefs(self.cnxn, 111)
        self.assertEqual(0, len(prefs_after.prefs))
    def testSetUserPrefs_Mine_Add(self):
        """User can set a preference for the first time."""
        request = users_pb2.SetUserPrefsRequest(prefs=[
            user_objects_pb2.UserPrefValue(name='code_font', value='true')
        ])
        mc = monorailcontext.MonorailContext(self.services,
                                             cnxn=self.cnxn,
                                             requester='*****@*****.**')
        self.CallWrapped(self.users_svcr.SetUserPrefs, mc, request)

        prefs_after = self.services.user.GetUserPrefs(self.cnxn, 111)
        self.assertEqual(1, len(prefs_after.prefs))
        self.assertEqual('code_font', prefs_after.prefs[0].name)
        self.assertEqual('true', prefs_after.prefs[0].value)
    def testSetUserPrefs_Mine_Overwrite(self):
        """User can change the value of a pref."""
        self.services.user.SetUserPrefs(
            self.cnxn, 111,
            [user_pb2.UserPrefValue(name='code_font', value='true')])
        request = users_pb2.SetUserPrefsRequest(prefs=[
            user_objects_pb2.UserPrefValue(name='code_font', value='false')
        ])
        mc = monorailcontext.MonorailContext(self.services,
                                             cnxn=self.cnxn,
                                             requester='*****@*****.**')
        self.CallWrapped(self.users_svcr.SetUserPrefs, mc, request)

        prefs_after = self.services.user.GetUserPrefs(self.cnxn, 111)
        self.assertEqual(1, len(prefs_after.prefs))
        self.assertEqual('code_font', prefs_after.prefs[0].name)
        self.assertEqual('false', prefs_after.prefs[0].value)
    def testSetUserPrefs_Other_Allowed(self):
        """A site admin can update another user's prefs."""
        self.services.user.SetUserPrefs(
            self.cnxn, 111,
            [user_pb2.UserPrefValue(name='code_font', value='true')])
        self.user_2.is_site_admin = True

        request = users_pb2.SetUserPrefsRequest(prefs=[
            user_objects_pb2.UserPrefValue(name='code_font', value='false')
        ])
        request.user_ref.display_name = '*****@*****.**'
        mc = monorailcontext.MonorailContext(self.services,
                                             cnxn=self.cnxn,
                                             requester='*****@*****.**')
        mc.LookupLoggedInUserPerms(self.project)
        self.CallWrapped(self.users_svcr.SetUserPrefs, mc, request)

        prefs_after = self.services.user.GetUserPrefs(self.cnxn, 111)
        self.assertEqual(1, len(prefs_after.prefs))
        self.assertEqual('code_font', prefs_after.prefs[0].name)
        self.assertEqual('false', prefs_after.prefs[0].value)
    def testSetUserPrefs_Other_Denied(self):
        """A non-admin cannot set another user's prefs."""
        self.services.user.SetUserPrefs(
            self.cnxn, 111,
            [user_pb2.UserPrefValue(name='code_font', value='true')])
        # user2 is not a site admin.

        request = users_pb2.SetUserPrefsRequest(prefs=[
            user_objects_pb2.UserPrefValue(name='code_font', value='false')
        ])
        request.user_ref.display_name = '*****@*****.**'
        mc = monorailcontext.MonorailContext(self.services,
                                             cnxn=self.cnxn,
                                             requester='*****@*****.**')
        mc.LookupLoggedInUserPerms(self.project)
        with self.assertRaises(permissions.PermissionException):
            self.CallWrapped(self.users_svcr.SetUserPrefs, mc, request)

        # Regardless of any exception, the preferences remain unchanged.
        prefs_after = self.services.user.GetUserPrefs(self.cnxn, 111)
        self.assertEqual(1, len(prefs_after.prefs))
        self.assertEqual('code_font', prefs_after.prefs[0].name)
        self.assertEqual('true', prefs_after.prefs[0].value)