This file is indexed.

/usr/lib/python3/dist-packages/gitlab/tests/test_mixins.py is in python3-gitlab 1:1.3.0-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# -*- coding: utf-8 -*-
#
# Copyright (C) 2014 Mika Mäenpää <mika.j.maenpaa@tut.fi>,
#                    Tampere University of Technology
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from __future__ import print_function

try:
    import unittest
except ImportError:
    import unittest2 as unittest

from httmock import HTTMock  # noqa
from httmock import response  # noqa
from httmock import urlmatch  # noqa

from gitlab import *  # noqa
from gitlab.base import *  # noqa
from gitlab.mixins import *  # noqa


class TestObjectMixinsAttributes(unittest.TestCase):
    def test_access_request_mixin(self):
        class O(AccessRequestMixin):
            pass

        obj = O()
        self.assertTrue(hasattr(obj, 'approve'))

    def test_subscribable_mixin(self):
        class O(SubscribableMixin):
            pass

        obj = O()
        self.assertTrue(hasattr(obj, 'subscribe'))
        self.assertTrue(hasattr(obj, 'unsubscribe'))

    def test_todo_mixin(self):
        class O(TodoMixin):
            pass

        obj = O()
        self.assertTrue(hasattr(obj, 'todo'))

    def test_time_tracking_mixin(self):
        class O(TimeTrackingMixin):
            pass

        obj = O()
        self.assertTrue(hasattr(obj, 'time_stats'))
        self.assertTrue(hasattr(obj, 'time_estimate'))
        self.assertTrue(hasattr(obj, 'reset_time_estimate'))
        self.assertTrue(hasattr(obj, 'add_spent_time'))
        self.assertTrue(hasattr(obj, 'reset_spent_time'))

    def test_set_mixin(self):
        class O(SetMixin):
            pass

        obj = O()
        self.assertTrue(hasattr(obj, 'set'))


class TestMetaMixins(unittest.TestCase):
    def test_retrieve_mixin(self):
        class M(RetrieveMixin):
            pass

        obj = M()
        self.assertTrue(hasattr(obj, 'list'))
        self.assertTrue(hasattr(obj, 'get'))
        self.assertFalse(hasattr(obj, 'create'))
        self.assertFalse(hasattr(obj, 'update'))
        self.assertFalse(hasattr(obj, 'delete'))
        self.assertIsInstance(obj, ListMixin)
        self.assertIsInstance(obj, GetMixin)

    def test_crud_mixin(self):
        class M(CRUDMixin):
            pass

        obj = M()
        self.assertTrue(hasattr(obj, 'get'))
        self.assertTrue(hasattr(obj, 'list'))
        self.assertTrue(hasattr(obj, 'create'))
        self.assertTrue(hasattr(obj, 'update'))
        self.assertTrue(hasattr(obj, 'delete'))
        self.assertIsInstance(obj, ListMixin)
        self.assertIsInstance(obj, GetMixin)
        self.assertIsInstance(obj, CreateMixin)
        self.assertIsInstance(obj, UpdateMixin)
        self.assertIsInstance(obj, DeleteMixin)

    def test_no_update_mixin(self):
        class M(NoUpdateMixin):
            pass

        obj = M()
        self.assertTrue(hasattr(obj, 'get'))
        self.assertTrue(hasattr(obj, 'list'))
        self.assertTrue(hasattr(obj, 'create'))
        self.assertFalse(hasattr(obj, 'update'))
        self.assertTrue(hasattr(obj, 'delete'))
        self.assertIsInstance(obj, ListMixin)
        self.assertIsInstance(obj, GetMixin)
        self.assertIsInstance(obj, CreateMixin)
        self.assertNotIsInstance(obj, UpdateMixin)
        self.assertIsInstance(obj, DeleteMixin)


class FakeObject(base.RESTObject):
    pass


class FakeManager(base.RESTManager):
    _path = '/tests'
    _obj_cls = FakeObject


class TestMixinMethods(unittest.TestCase):
    def setUp(self):
        self.gl = Gitlab("http://localhost", private_token="private_token",
                         api_version=4)

    def test_get_mixin(self):
        class M(GetMixin, FakeManager):
            pass

        @urlmatch(scheme="http", netloc="localhost", path='/api/v4/tests/42',
                  method="get")
        def resp_cont(url, request):
            headers = {'Content-Type': 'application/json'}
            content = '{"id": 42, "foo": "bar"}'
            return response(200, content, headers, None, 5, request)

        with HTTMock(resp_cont):
            mgr = M(self.gl)
            obj = mgr.get(42)
            self.assertIsInstance(obj, FakeObject)
            self.assertEqual(obj.foo, 'bar')
            self.assertEqual(obj.id, 42)

    def test_get_without_id_mixin(self):
        class M(GetWithoutIdMixin, FakeManager):
            pass

        @urlmatch(scheme="http", netloc="localhost", path='/api/v4/tests',
                  method="get")
        def resp_cont(url, request):
            headers = {'Content-Type': 'application/json'}
            content = '{"foo": "bar"}'
            return response(200, content, headers, None, 5, request)

        with HTTMock(resp_cont):
            mgr = M(self.gl)
            obj = mgr.get()
            self.assertIsInstance(obj, FakeObject)
            self.assertEqual(obj.foo, 'bar')
            self.assertFalse(hasattr(obj, 'id'))

    def test_list_mixin(self):
        class M(ListMixin, FakeManager):
            pass

        @urlmatch(scheme="http", netloc="localhost", path='/api/v4/tests',
                  method="get")
        def resp_cont(url, request):
            headers = {'Content-Type': 'application/json'}
            content = '[{"id": 42, "foo": "bar"},{"id": 43, "foo": "baz"}]'
            return response(200, content, headers, None, 5, request)

        with HTTMock(resp_cont):
            # test RESTObjectList
            mgr = M(self.gl)
            obj_list = mgr.list(as_list=False)
            self.assertIsInstance(obj_list, base.RESTObjectList)
            for obj in obj_list:
                self.assertIsInstance(obj, FakeObject)
                self.assertIn(obj.id, (42, 43))

            # test list()
            obj_list = mgr.list(all=True)
            self.assertIsInstance(obj_list, list)
            self.assertEqual(obj_list[0].id, 42)
            self.assertEqual(obj_list[1].id, 43)
            self.assertIsInstance(obj_list[0], FakeObject)
            self.assertEqual(len(obj_list), 2)

    def test_list_other_url(self):
        class M(ListMixin, FakeManager):
            pass

        @urlmatch(scheme="http", netloc="localhost", path='/api/v4/others',
                  method="get")
        def resp_cont(url, request):
            headers = {'Content-Type': 'application/json'}
            content = '[{"id": 42, "foo": "bar"}]'
            return response(200, content, headers, None, 5, request)

        with HTTMock(resp_cont):
            mgr = M(self.gl)
            obj_list = mgr.list(path='/others', as_list=False)
            self.assertIsInstance(obj_list, base.RESTObjectList)
            obj = obj_list.next()
            self.assertEqual(obj.id, 42)
            self.assertEqual(obj.foo, 'bar')
            self.assertRaises(StopIteration, obj_list.next)

    def test_get_from_list_mixin(self):
        class M(GetFromListMixin, FakeManager):
            pass

        @urlmatch(scheme="http", netloc="localhost", path='/api/v4/tests',
                  method="get")
        def resp_cont(url, request):
            headers = {'Content-Type': 'application/json'}
            content = '[{"id": 42, "foo": "bar"},{"id": 43, "foo": "baz"}]'
            return response(200, content, headers, None, 5, request)

        with HTTMock(resp_cont):
            mgr = M(self.gl)
            obj = mgr.get(42)
            self.assertIsInstance(obj, FakeObject)
            self.assertEqual(obj.foo, 'bar')
            self.assertEqual(obj.id, 42)

            self.assertRaises(GitlabGetError, mgr.get, 44)

    def test_create_mixin_get_attrs(self):
        class M1(CreateMixin, FakeManager):
            pass

        class M2(CreateMixin, FakeManager):
            _create_attrs = (('foo',), ('bar', 'baz'))
            _update_attrs = (('foo',), ('bam', ))

        mgr = M1(self.gl)
        required, optional = mgr.get_create_attrs()
        self.assertEqual(len(required), 0)
        self.assertEqual(len(optional), 0)

        mgr = M2(self.gl)
        required, optional = mgr.get_create_attrs()
        self.assertIn('foo', required)
        self.assertIn('bar', optional)
        self.assertIn('baz', optional)
        self.assertNotIn('bam', optional)

    def test_create_mixin_missing_attrs(self):
        class M(CreateMixin, FakeManager):
            _create_attrs = (('foo',), ('bar', 'baz'))

        mgr = M(self.gl)
        data = {'foo': 'bar', 'baz': 'blah'}
        mgr._check_missing_create_attrs(data)

        data = {'baz': 'blah'}
        with self.assertRaises(AttributeError) as error:
            mgr._check_missing_create_attrs(data)
        self.assertIn('foo', str(error.exception))

    def test_create_mixin(self):
        class M(CreateMixin, FakeManager):
            _create_attrs = (('foo',), ('bar', 'baz'))
            _update_attrs = (('foo',), ('bam', ))

        @urlmatch(scheme="http", netloc="localhost", path='/api/v4/tests',
                  method="post")
        def resp_cont(url, request):
            headers = {'Content-Type': 'application/json'}
            content = '{"id": 42, "foo": "bar"}'
            return response(200, content, headers, None, 5, request)

        with HTTMock(resp_cont):
            mgr = M(self.gl)
            obj = mgr.create({'foo': 'bar'})
            self.assertIsInstance(obj, FakeObject)
            self.assertEqual(obj.id, 42)
            self.assertEqual(obj.foo, 'bar')

    def test_create_mixin_custom_path(self):
        class M(CreateMixin, FakeManager):
            _create_attrs = (('foo',), ('bar', 'baz'))
            _update_attrs = (('foo',), ('bam', ))

        @urlmatch(scheme="http", netloc="localhost", path='/api/v4/others',
                  method="post")
        def resp_cont(url, request):
            headers = {'Content-Type': 'application/json'}
            content = '{"id": 42, "foo": "bar"}'
            return response(200, content, headers, None, 5, request)

        with HTTMock(resp_cont):
            mgr = M(self.gl)
            obj = mgr.create({'foo': 'bar'}, path='/others')
            self.assertIsInstance(obj, FakeObject)
            self.assertEqual(obj.id, 42)
            self.assertEqual(obj.foo, 'bar')

    def test_update_mixin_get_attrs(self):
        class M1(UpdateMixin, FakeManager):
            pass

        class M2(UpdateMixin, FakeManager):
            _create_attrs = (('foo',), ('bar', 'baz'))
            _update_attrs = (('foo',), ('bam', ))

        mgr = M1(self.gl)
        required, optional = mgr.get_update_attrs()
        self.assertEqual(len(required), 0)
        self.assertEqual(len(optional), 0)

        mgr = M2(self.gl)
        required, optional = mgr.get_update_attrs()
        self.assertIn('foo', required)
        self.assertIn('bam', optional)
        self.assertNotIn('bar', optional)
        self.assertNotIn('baz', optional)

    def test_update_mixin_missing_attrs(self):
        class M(UpdateMixin, FakeManager):
            _update_attrs = (('foo',), ('bar', 'baz'))

        mgr = M(self.gl)
        data = {'foo': 'bar', 'baz': 'blah'}
        mgr._check_missing_update_attrs(data)

        data = {'baz': 'blah'}
        with self.assertRaises(AttributeError) as error:
            mgr._check_missing_update_attrs(data)
        self.assertIn('foo', str(error.exception))

    def test_update_mixin(self):
        class M(UpdateMixin, FakeManager):
            _create_attrs = (('foo',), ('bar', 'baz'))
            _update_attrs = (('foo',), ('bam', ))

        @urlmatch(scheme="http", netloc="localhost", path='/api/v4/tests/42',
                  method="put")
        def resp_cont(url, request):
            headers = {'Content-Type': 'application/json'}
            content = '{"id": 42, "foo": "baz"}'
            return response(200, content, headers, None, 5, request)

        with HTTMock(resp_cont):
            mgr = M(self.gl)
            server_data = mgr.update(42, {'foo': 'baz'})
            self.assertIsInstance(server_data, dict)
            self.assertEqual(server_data['id'], 42)
            self.assertEqual(server_data['foo'], 'baz')

    def test_update_mixin_no_id(self):
        class M(UpdateMixin, FakeManager):
            _create_attrs = (('foo',), ('bar', 'baz'))
            _update_attrs = (('foo',), ('bam', ))

        @urlmatch(scheme="http", netloc="localhost", path='/api/v4/tests',
                  method="put")
        def resp_cont(url, request):
            headers = {'Content-Type': 'application/json'}
            content = '{"foo": "baz"}'
            return response(200, content, headers, None, 5, request)

        with HTTMock(resp_cont):
            mgr = M(self.gl)
            server_data = mgr.update(new_data={'foo': 'baz'})
            self.assertIsInstance(server_data, dict)
            self.assertEqual(server_data['foo'], 'baz')

    def test_delete_mixin(self):
        class M(DeleteMixin, FakeManager):
            pass

        @urlmatch(scheme="http", netloc="localhost", path='/api/v4/tests/42',
                  method="delete")
        def resp_cont(url, request):
            headers = {'Content-Type': 'application/json'}
            content = ''
            return response(200, content, headers, None, 5, request)

        with HTTMock(resp_cont):
            mgr = M(self.gl)
            mgr.delete(42)

    def test_save_mixin(self):
        class M(UpdateMixin, FakeManager):
            pass

        class O(SaveMixin, RESTObject):
            pass

        @urlmatch(scheme="http", netloc="localhost", path='/api/v4/tests/42',
                  method="put")
        def resp_cont(url, request):
            headers = {'Content-Type': 'application/json'}
            content = '{"id": 42, "foo": "baz"}'
            return response(200, content, headers, None, 5, request)

        with HTTMock(resp_cont):
            mgr = M(self.gl)
            obj = O(mgr, {'id': 42, 'foo': 'bar'})
            obj.foo = 'baz'
            obj.save()
            self.assertEqual(obj._attrs['foo'], 'baz')
            self.assertDictEqual(obj._updated_attrs, {})

    def test_set_mixin(self):
        class M(SetMixin, FakeManager):
            pass

        @urlmatch(scheme="http", netloc="localhost", path='/api/v4/tests/foo',
                  method="put")
        def resp_cont(url, request):
            headers = {'Content-Type': 'application/json'}
            content = '{"key": "foo", "value": "bar"}'
            return response(200, content, headers, None, 5, request)

        with HTTMock(resp_cont):
            mgr = M(self.gl)
            obj = mgr.set('foo', 'bar')
            self.assertIsInstance(obj, FakeObject)
            self.assertEqual(obj.key, 'foo')
            self.assertEqual(obj.value, 'bar')