~launchpad-pqm/launchpad/devel

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
# Copyright 2009 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

import unittest

import transaction
from zope.component import getUtility

from lp.services.librarian.model import LibraryFileContent
from lp.services.librarianserver import db
from lp.services.webapp.interfaces import (
    DEFAULT_FLAVOR,
    IStoreSelector,
    MAIN_STORE,
    )
from lp.testing.layers import LaunchpadZopelessLayer


class DBTestCase(unittest.TestCase):
    layer = LaunchpadZopelessLayer

    def setUp(self):
        self.layer.switchDbUser('librarian')

    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')
        self.assertEqual([fileID], library.lookupBySHA1('deadbeef'))

        # Add a new file with the same digest
        newFileID = library.add('deadbeef', 1234, 'abababab')
        # 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)


class TestLibrarianStuff(unittest.TestCase):
    """Tests for the librarian."""

    layer = LaunchpadZopelessLayer

    def setUp(self):
        self.layer.switchDbUser('librarian')
        self.store = getUtility(IStoreSelector).get(
                MAIN_STORE, DEFAULT_FLAVOR)
        self.content_id = db.Library().add('deadbeef', 1234, 'abababab')
        self.file_content = self._getTestFileContent()
        transaction.commit()

    def _getTestFileContent(self):
        """Return the file content object that created."""
        return self.store.find(LibraryFileContent, id=self.content_id).one()

    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)

    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, '/')

    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, '/')

    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, '/')

    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, '/')

    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)

    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)

    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)