示例#1
0
 def test_getAlias_with_wrong_macaroon(self):
     # A macaroon for a different LFA doesn't work.
     authserver = self.setUpAuthServer()
     unrestricted_library = db.Library(restricted=False)
     alias = unrestricted_library.getAlias(1, None, '/')
     alias.restricted = True
     transaction.commit()
     macaroon = Macaroon()
     authserver.registerMacaroon(macaroon, 2)
     restricted_library = db.Library(restricted=True)
     with ExpectedException(LookupError):
         yield deferToThread(restricted_library.getAlias, 1, macaroon, '/')
示例#2
0
    def test_getAlias_content_wrong_library(self):
        # Library.getAlias() raises a LookupError, if a restricted
        # library looks up a unrestricted LibraryFileAlias and
        # vice versa.
        restricted_library = db.Library(restricted=True)
        self.assertRaises(LookupError, restricted_library.getAlias, 1, None,
                          '/')

        unrestricted_library = db.Library(restricted=False)
        alias = unrestricted_library.getAlias(1, None, '/')
        alias.restricted = True
        self.assertRaises(LookupError, unrestricted_library.getAlias, 1, None,
                          '/')
示例#3
0
 def test_getAlias_with_macaroon(self):
     # Library.getAlias() uses the authserver to verify macaroons.
     authserver = self.setUpAuthServer()
     unrestricted_library = db.Library(restricted=False)
     alias = unrestricted_library.getAlias(1, None, '/')
     alias.restricted = True
     transaction.commit()
     restricted_library = db.Library(restricted=True)
     macaroon = Macaroon()
     with ExpectedException(LookupError):
         yield deferToThread(restricted_library.getAlias, 1, macaroon, '/')
     authserver.registerMacaroon(macaroon, 1)
     alias = yield deferToThread(restricted_library.getAlias, 1, macaroon,
                                 '/')
     self.assertEqual(1, alias.id)
示例#4
0
 def test_getAlias_content_is_none(self):
     # Library.getAlias() raises a LookupError, if the matching
     # record does not reference any LibraryFileContent record.
     library = db.Library(restricted=False)
     alias = library.getAlias(1, None, '/')
     alias.content = None
     self.assertRaises(LookupError, library.getAlias, 1, None, '/')
示例#5
0
 def test_getAlias_content_is_null(self):
     # Library.getAlias() raises a LookupError, if no content
     # record for the given alias exists.
     library = db.Library(restricted=False)
     alias = library.getAlias(1, None, '/')
     alias.content = None
     self.assertRaises(LookupError, library.getAlias, 1, None, '/')
示例#6
0
 def setUp(self):
     super(TestLibrarianStuff, self).setUp()
     switch_dbuser('librarian')
     self.store = IStore(LibraryFileContent)
     self.content_id = db.Library().add('deadbeef', 1234, 'abababab', 'ba')
     self.file_content = self._getTestFileContent()
     transaction.commit()
示例#7
0
 def test_getAlias_with_macaroon_timeout(self):
     # The authserver call is cancelled after a timeout period.
     unrestricted_library = db.Library(restricted=False)
     alias = unrestricted_library.getAlias(1, None, '/')
     alias.restricted = True
     transaction.commit()
     macaroon = Macaroon()
     restricted_library = db.Library(restricted=True)
     self.useFixture(
         MockPatchObject(restricted_library._authserver,
                         'callRemote',
                         return_value=defer.Deferred()))
     # XXX cjwatson 2018-11-01: We should use a Clock instead, but I had
     # trouble getting that working in conjunction with deferToThread.
     self.pushConfig('librarian', authentication_timeout=1)
     with ExpectedException(defer.CancelledError):
         yield deferToThread(restricted_library.getAlias, 1, macaroon, '/')
示例#8
0
    def test_getAliases_content_wrong_library(self):
        # Library.getAliases() does not return data from restriceded
        # LibrarayFileAlias records when called from a unrestricted
        # library and vice versa.
        unrestricted_library = db.Library(restricted=False)
        alias = unrestricted_library.getAlias(1, None, '/')
        alias.restricted = True

        aliases = unrestricted_library.getAliases(1)
        expected_aliases = [
            (2, u'netapplet_1.0.0.orig.tar.gz', u'application/x-gtar'),
        ]
        self.assertEqual(expected_aliases, aliases)

        restricted_library = db.Library(restricted=True)
        aliases = restricted_library.getAliases(1)
        expected_aliases = [
            (1, u'netapplet-1.0.0.tar.gz', u'application/x-gtar'),
        ]
        self.assertEqual(expected_aliases, aliases)
示例#9
0
 def test_getAliases_content_is_none(self):
     # Library.getAliases() does not return records which do not
     # reference any LibraryFileContent record.
     library = db.Library(restricted=False)
     alias = library.getAlias(1, None, '/')
     alias.content = None
     aliases = library.getAliases(1)
     expected_aliases = [
         (2, u'netapplet_1.0.0.orig.tar.gz', u'application/x-gtar'),
     ]
     self.assertEqual(expected_aliases, aliases)
示例#10
0
 def test_getAliases(self):
     # Library.getAliases() returns a sequence
     # [(LFA.id, LFA.filename, LFA.mimetype), ...] where LFA are
     # LibrarayFileAlias records having the given LibraryFileContent
     # ID.
     library = db.Library(restricted=False)
     aliases = library.getAliases(1)
     expected_aliases = [
         (1, u'netapplet-1.0.0.tar.gz', u'application/x-gtar'),
         (2, u'netapplet_1.0.0.orig.tar.gz', u'application/x-gtar'),
     ]
     self.assertEqual(expected_aliases, aliases)
示例#11
0
    def setUp(self):
        self.directory = tempfile.mkdtemp()
        self.storage = LibrarianStorage(self.directory, db.Library())

        # Hook the commit and rollback methods of the store.
        self.store = IStore(LibraryFileContent)
        self.committed = self.rolledback = False
        self.orig_commit = self.store.commit
        self.orig_rollback = self.store.rollback

        def commit():
            self.committed = True
            self.orig_commit()

        self.store.commit = commit

        def rollback():
            self.rolledback = True
            self.orig_rollback()

        self.store.rollback = rollback
示例#12
0
    def test_lookupByDigest(self):
        # Create library
        library = db.Library()

        # Initially it should be empty
        self.assertEqual([], library.lookupBySHA1('deadbeef'))

        # Add a file, check it is found by lookupBySHA1
        fileID = library.add('deadbeef', 1234, 'abababab', 'babababa')
        self.assertEqual([fileID], library.lookupBySHA1('deadbeef'))

        # Add a new file with the same digest
        newFileID = library.add('deadbeef', 1234, 'abababab', 'babababa')
        # Check it gets a new ID anyway
        self.assertNotEqual(fileID, newFileID)
        # Check it is found by lookupBySHA1
        self.assertEqual(sorted([fileID, newFileID]),
                         sorted(library.lookupBySHA1('deadbeef')))

        aliasID = library.addAlias(fileID, 'file1', 'text/unknown')
        alias = library.getAlias(aliasID, None, '/')
        self.assertEqual('file1', alias.filename)
        self.assertEqual('text/unknown', alias.mimetype)
 def setUp(self):
     switch_dbuser('librarian')
     self.directory = tempfile.mkdtemp()
     self.storage = LibrarianStorage(self.directory, db.Library())
示例#14
0
 def test_getAlias_no_such_record(self):
     # Library.getAlias() raises a LookupError, if no record with
     # the given ID exists.
     library = db.Library(restricted=False)
     self.assertRaises(LookupError, library.getAlias, -1, None, '/')
示例#15
0
 def test_getAlias(self):
     # Library.getAlias() returns the LibrarayFileAlias for a given
     # LibrarayFileAlias ID.
     library = db.Library(restricted=False)
     alias = library.getAlias(1, None, '/')
     self.assertEqual(1, alias.id)