コード例 #1
0
 def setUp(self):
     self.config = testing.setUp()
     self.logger = Logging(self.settings, None)
     self.storage = Storage(config=self.settings, logger=self.logger)
     self.counter = self.storage.counter
     records = []
     for diff in self.diffs:
         record = self.base_record.copy()
         record.update(diff)
         records.append(record)
     self.storage.put_announce(records, now=self.now)
コード例 #2
0
 def setUp(self):
     self.config = testing.setUp()
     tsettings = TConfig({
         'db.type': 'sqlite',
         'db.db': '/tmp/test.db',
         'logging.use_metlog': False
     })
     self.logger = Logging(tsettings, None)
     self.storage = Storage(config=tsettings, logger=self.logger)
     self.counter = Counter(config=tsettings, logger=self.logger)
     records = []
     for diff in self.diffs:
         record = self.base_record.copy()
         record.update(diff)
         records.append(record)
     self.storage.put_announce(records, now=self.now)
コード例 #3
0
 def setUp(self):
     self.config = testing.setUp()
     self.logger = Logging(self.settings, None)
     self.storage = Storage(config=self.settings, logger=self.logger)
     self.counter = self.storage.counter
     records = []
     for diff in self.diffs:
         record = self.base_record.copy()
         record.update(diff)
         records.append(record)
     self.storage.put_announce(records, now=self.now)
コード例 #4
0
 def setUp(self):
     self.config = testing.setUp()
     tsettings = TConfig({'db.type': 'sqlite',
                          'db.db': '/tmp/test.db',
                          'logging.use_metlog': False})
     self.storage = Storage(config=tsettings)
     self.logger = Logging(tsettings, None)
     records = []
     for diff in self.diffs:
         record = self.base_record.copy()
         record.update(diff)
         records.append(record)
     self.storage.put_announce(records, now=self.now)
コード例 #5
0
class TestStorage(unittest2.TestCase):

    now = 10000

    test_announce = {
        'start_time': int(now - 300),
        'end_time': int(now + 3000),
        'lang': 'en',
        'locale': 'US',
        'body': 'Text Body',
        'title': 'Test',
        'dest_url': 'http://example.com'
    }

    config = TConfig({
        'db.type': 'sqlite',
        'db.db': '/tmp/test.db',
        'logging.use_metlog': False
    })
    logger = Logging(config, None)

    def setUp(self):
        self.storage = Storage(config=self.config, logger=self.logger)

    def tearDown(self):
        self.storage.purge()
        del self.storage

    def test_health_check(self):
        result = self.storage.health_check()
        self.assertEquals(result, True)

    def test_announcement(self):
        self.storage.put_announce(self.test_announce, now=self.now)
        items = self.storage.get_all_announce()
        self.failUnless(len(items) > 0)
        self.failUnless('text' in items[0]['note'])
        self.failUnless('title' in items[0]['note'])
        self.failUnless(self.test_announce['body'] in items[0]['note'])
        self.failUnless(self.test_announce['title'] in items[0]['note'])
        self.failUnless(self.test_announce['dest_url'] in items[0]['dest_url'])

    def update_note(self, announce, note_text):
        return announce.copy()

    def reload(self):
        records = []
        updates = [
            {
                'lang': None,
                'locale': None,
                'title': 'Everyone'
            },
            {
                'platform': 'a',
                'channel': 'a',
                'title': 'p:a;c:a'
            },
            {
                'platform': 'b',
                'channel': 'a',
                'title': 'p:b;c:a'
            },
            {
                'platform': 'a',
                'start_time': self.now + 20,
                'end_time': self.now + 3000,
                'title': 'notyet'
            },
            #{'platform': 'a', 'end_time': self.now - 50,
            # 'title': 'tooold'},
            {
                'platform': 'a',
                'idle_time': 10,
                'title': 'idle: 10'
            },
            {
                'platform': 'a',
                'channel': 'b',
                'lang': 'a',
                'locale': 'a',
                'idle_time': 10,
                'title': 'full_rec'
            }
        ]
        # load the database
        for update in updates:
            test = self.test_announce.copy()
            test.update(update)
            records.append(test)
        self.storage.put_announce(records, now=self.now)
        #time.sleep(3);

    def test_search0(self):
        """ Yes, this test does a lot of things. That's because I need
        to exercise the search algo using a lot of records. """
        # really wish that update allowed chaining.
        self.reload()
        data = {'platform': 'f', 'channel': 'f', 'version': 0}
        announce = self.storage.get_announce(data, now=self.now + 86400 * 2)
        self.assertEqual(len(announce), 1)
        self.assertEqual(announce[0]['title'], 'Everyone')

    def test_search1(self):
        self.reload()
        data = {'platform': 'a', 'channel': 'a'}
        print "P&C check:"
        announce = self.storage.get_announce(data, now=self.now + 10)
        # only Everyone and p: a;c: a should be returned.
        self.assertEqual(len(announce), 2)
        # Make sure the most specific entry is returned first.
        self.assertEqual(announce[0].get('title'), 'p:a;c:a')

    def test_search2(self):
        self.reload()
        data = {'platform': 'a', 'channel': 'a', 'idle': 15}
        print "Idle Check:"
        announce = self.storage.get_announce(data, now=self.now)
        self.assertEqual(len(announce), 3)

    def test_search3(self):
        self.reload()
        data = {'platform': 'a', 'channel': 'b'}
        announce = self.storage.get_announce(data, now=self.now + 10)
        print "P&C2 check:"
        self.assertEqual(len(announce), 1)
        # Store the unique record data for the resolve check.
        resolve_rec = announce[0]
        goat_id = resolve_rec['url'].split('/').pop()

        data = {'platform': 'a', 'channel': 'a'}
        print self.now + 21
        print "Wake check: %s " % (self.now + 21)
        announce = self.storage.get_announce(data, now=self.now + 21)
        self.assertEqual(len(announce), 3)

        # Since we have an ID for a unique record, query it to make
        # sure records resolve.
        print "resolve check: %s" % goat_id
        rec = self.storage.resolve(goat_id)
        self.assertEqual('Everyone', json.loads(rec['note'])['title'])
コード例 #6
0
 def setUp(self):
     self.storage = Storage(config=self.config, logger=self.logger)
コード例 #7
0
class TestStorage(unittest2.TestCase):

    now = 10000

    test_announce = {
        'start_time': int(now - 300),
        'end_time': int(now + 3000),
        'lang': 'en',
        'locale': 'US',
        'body': 'Text Body',
        'title': 'Test',
        'dest_url': 'http://example.com'
    }

    def setUp(self):
        self.storage = Storage(config=TConfig({'db.type': 'sqlite',
            'db.db': '/tmp/test.db'}))

    def tearDown(self):
        self.storage.purge()
        del self.storage

    def test_health_check(self):
        result = self.storage.health_check()
        self.assertEquals(result, True)

    def test_announcement(self):
        self.storage.put_announce(self.test_announce, now=self.now)
        items = self.storage.get_all_announce()
        self.failUnless(len(items) > 0)
        self.failUnless('text' in items[0]['note'])
        self.failUnless('title' in items[0]['note'])
        self.failUnless(self.test_announce['body'] in items[0]['note'])
        self.failUnless(self.test_announce['title'] in items[0]['note'])
        self.failUnless(self.test_announce['dest_url'] in items[0]['dest_url'])

    def update_note(self, announce, note_text):
        return announce.copy()

    def reload(self):
        records = []
        updates = [{'lang': None, 'locale': None, 'title': 'Everyone'},
            {'platform': 'a', 'channel': 'a', 'title': 'p:a;c:a'},
            {'platform': 'b', 'channel': 'a', 'title': 'p:b;c:a'},
            {'platform': 'a', 'start_time': self.now + 20,
                'end_time': self.now + 3000, 'title': 'notyet'},
            #{'platform': 'a', 'end_time': self.now - 50, 'title': 'tooold'},
            {'platform': 'a', 'idle_time': 10, 'title': 'idle: 10'},
            {'platform': 'a', 'channel': 'b', 'lang': 'a', 'locale': 'a',
                'idle_time': 10, 'title': 'full_rec'}
        ]
        # load the database
        for update in updates:
            test = self.test_announce.copy()
            test.update(update)
            records.append(test)
        self.storage.put_announce(records, now=self.now)
        #time.sleep(3);

    def test_search0(self):
        """ Yes, this test does a lot of things. That's because I need
        to exercise the search algo using a lot of records. """
        # really wish that update allowed chaining.
        self.reload()
        data = {'platform': 'f', 'channel': 'f', 'version': 0}
        announce = self.storage.get_announce(data, now=self.now + 86400 * 2)
        self.assertEqual(len(announce), 1)
        self.assertEqual(announce[0]['title'], 'Everyone')

    def test_search1(self):
        self.reload()
        data = {'platform': 'a', 'channel': 'a'}
        print "P&C check:"
        announce = self.storage.get_announce(data, now=self.now + 10)
        # only Everyone and p: a;c: a should be returned.
        self.assertEqual(len(announce), 2)
        # Make sure the most specific entry is returned first.
        self.assertEqual(announce[0].get('title'), 'p:a;c:a')

    def test_search2(self):
        self.reload()
        data = {'platform': 'a', 'channel': 'a', 'idle': 15}
        print "Idle Check:"
        announce = self.storage.get_announce(data, now=self.now)
        self.assertEqual(len(announce), 3)

    def test_search3(self):
        self.reload()
        data = {'platform': 'a', 'channel': 'b'}
        announce = self.storage.get_announce(data, now=self.now + 10)
        print "P&C2 check:"
        self.assertEqual(len(announce), 1)
        # Store the unique record data for the resolve check.
        resolve_rec = announce[0]
        goat_id = resolve_rec['url'].split('/').pop()

        data = {'platform': 'a', 'channel': 'a'}
        print self.now + 21
        print "Wake check: %s " % (self.now + 21)
        announce = self.storage.get_announce(data, now=self.now + 21)
        self.assertEqual(len(announce), 3)

        # Since we have an ID for a unique record, query it to make
        # sure records resolve.
        print "resolve check: %s" % goat_id
        rec = self.storage.resolve(goat_id)
        self.assertEqual('Everyone', json.loads(rec['note'])['title'])
コード例 #8
0
 def setUp(self):
     self.storage = Storage(config=TConfig({'db.type': 'sqlite',
         'db.db': '/tmp/test.db'}))
コード例 #9
0
class ViewTest(unittest2.TestCase):

    now = 10000

    base_record = {
        'start_time': int(now),
        'end_time': int(now + 3000),
        'lang': 'en',
        'locale': 'US',
        'body': 'Body',
        'title': 'Title',
        'dest_url': 'http://example.com'
    }

    diffs = [{
        'channel': None,
        'platform': None,
        'version': None,
        'title': 'all'
    }, {
        'channel': 'a',
        'platform': None,
        'version': None,
        'title': 'ann'
    }, {
        'channel': 'a',
        'platform': 'a',
        'version': 0,
        'title': 'aa0'
    }, {
        'channel': 'a',
        'platform': 'a',
        'version': 0,
        'idle_time': 1,
        'title': 'aa0i1'
    }, {
        'channel': 'a',
        'platform': 'b',
        'version': 0,
        'title': 'ab0'
    }, {
        'channel': 'b',
        'platform': 'a',
        'version': 2,
        'title': 'ba2',
        'dest_url': 'http://example.org'
    }, {
        'channel':
        'e',
        'platform':
        None,
        'version':
        None,
        'title':
        'utf8',
        'body':
        "\u0192\u00a9\u02d9\u02da\u02da\u00ac\u2206\u02d9\u02da\u02d9\u00a9\u2206\u00a9\u0192\u00df \u0376\u0376\u0376\u0376\u0376\u0376\u0376\u0376\u0376\u0376\u0376\u0376\u0376\u0376"
    }]

    settings = TConfig({
        'db.type': 'sqlite',
        'db.db': '/tmp/test.db',
        'logging.use_heka': False,
        'db.checkAccount': False
    })

    def req(self, matchdict={}, user_id=None, headers=None, **kw):
        class Reg(dict):

            settings = {}

            def __init__(self, settings=None, **kw):
                super(Reg, self).__init__(**kw)
                if settings:
                    self.settings = settings

        request = Request(headers=headers, **kw)
        request.GET = kw.get('params', {})
        request.registry = Reg(settings=self.config.get_settings())
        request.registry['storage'] = self.storage
        request.registry['logger'] = self.logger
        request.registry['counter'] = self.counter
        request.registry['auth'] = mock.Mock()
        request.registry['auth'].get_user_id.return_value = user_id
        request.registry.settings.update(self.settings.settings)
        if matchdict:
            request.matchdict.update(matchdict)
        return request

    def setUp(self):
        self.config = testing.setUp()
        self.logger = Logging(self.settings, None)
        self.storage = Storage(config=self.settings, logger=self.logger)
        self.counter = self.storage.counter
        records = []
        for diff in self.diffs:
            record = self.base_record.copy()
            record.update(diff)
            records.append(record)
        self.storage.put_announce(records, now=self.now)
        #time.sleep(3);

    def tearDown(self):
        self.storage.purge()

    def test_get_announcements(self):
        # normal number
        response = views.get_announcements(
            self.req(matchdict={
                'channel': 'a',
                'platform': 'a',
                'version': 0
            }),
            now=self.now)
        eq_(len(json.loads(response.body)['announcements']), 3)
        assert ('Date' in response.headers)
        response = views.get_announcements(self.req(
            matchdict={
                'channel': 'a',
                'platform': 'a',
                'version': 0
            },
            headers={'Accept-Language': 'en'}),
                                           now=self.now)
        eq_(len(json.loads(response.body)['announcements']), 3)
        # idle number
        response = views.get_announcements(
            self.req(matchdict={
                'channel': 'a',
                'platform': 'a',
                'version': 0
            },
                     params={'idle': '6'}))
        eq_(len(json.loads(response.body)['announcements']), 4)
        timestamp = time.strftime("%a, %d %b %Y %H:%M:%S GMT",
                                  time.gmtime(self.now + 1))
        self.assertRaises(
            http.HTTPNotModified, views.get_announcements,
            self.req(matchdict={
                'channel': 'a',
                'platform': 'a',
                'version': 0
            },
                     headers={'If-Modified-Since': timestamp}))
        self.storage.purge()
        self.assertRaises(
            http.HTTPNoContent, views.get_announcements,
            self.req(matchdict={
                'channel': 'z',
                'platform': 'z',
                'version': 'z'
            }))

    def test_get_all(self):
        self.assertRaises(http.HTTPUnauthorized, views.get_all_announcements,
                          self.req())
        # scan for include.js or 'test="login"' id?
        # try with a 'valid' user id
        self.assertRaises(
            http.HTTPUnauthorized, views.get_all_announcements,
            self.req(matchdict={}, user_id='*****@*****.**'))
        # try successful json
        req = self.req(matchdict={}, user_id='*****@*****.**')
        req.accept_encoding = 'application/javascript'
        response = views.get_all_announcements(req)
        eq_(len(response['announcements']), 7)

    def test_get_lang_loc(self):
        response = views.get_lang_loc(
            self.req(headers={'Accept-Language': 'en_US'}))
        eq_(response, {'lang': 'en', 'locale': 'US'})
        response = views.get_lang_loc(
            self.req(headers={'Accept-Language': 'en-US'}))
        eq_(response, {'lang': 'en', 'locale': 'US'})
        response = views.get_lang_loc(
            self.req(headers={'Accept-Language': 'en'}))
        eq_(response, {'lang': 'en', 'locale': None})

    def test_handle_redir(self):
        # get a record
        response = self.storage.get_announce({'channel': 'b'})
        record = response[0]
        rec_id = record['url'].split('/').pop()
        req = self.req(matchdict={'token': rec_id})
        self.assertRaises(http.HTTPTemporaryRedirect, views.handle_redir, req)
        self.assertRaises(http.HTTPNotFound, views.handle_redir,
                          self.req(matchdict={'token': 'Invalid Token'}))

    def test_admin_page(self):
        req = self.req()
        response = views.admin_page(req)
        eq_(response.status_code, 403)
        req = self.req(matchdict={}, user_id='*****@*****.**')
        response = views.admin_page(req)
        eq_(response.status_code, 200)
        req.registry.settings.update({'auth.block_authoring': True})
        self.assertRaises(http.HTTPNotFound, views.admin_page, req)

    def test_manage_announce(self):
        # test assertion post
        req = self.req(matchdict={
            'channel': 'c',
            'title': 'Goat',
            'body': 'Ready for sacrifice'
        },
                       user_id='*****@*****.**')
        response = views.manage_announce(req)
        # test create
        ann = views.get_announcements(self.req(matchdict={'channel': 'c'}))
        response = json.loads(ann.body)
        goat = None
        for record in response['announcements']:
            if record['title'] == 'Goat':
                goat = record
                break
        goat_id = goat['url'].split('/').pop()
        self.assertIsNotNone(goat)
        req = self.req(params={'delete': goat_id}, user_id='*****@*****.**')
        self.assertRaises(http.HTTPOk, views.del_announce, req)
        time.sleep(2)  # Give the db a second to write the record.
        req = self.req(matchdict={'token': goat_id})
        self.assertRaises(http.HTTPNotFound, views.handle_redir, req)
コード例 #10
0
 def setUp(self):
     self.storage = Storage(config=self.config, logger=self.logger)
コード例 #11
0
class ViewTest(unittest2.TestCase):

    now = 10000

    base_record = {
        'start_time': int(now),
        'end_time': int(now + 3000),
        'lang': 'en',
        'locale': 'US',
        'body': 'Body',
        'title': 'Title',
        'dest_url': 'http://example.com'
    }

    diffs = [
        {'channel': None, 'platform': None, 'version': None, 'title': 'all'},
        {'channel': 'a', 'platform': None, 'version': None, 'title': 'ann'},
        {'channel': 'a', 'platform': 'a', 'version': 0, 'title': 'aa0'},
        {'channel': 'a', 'platform': 'a', 'version': 0, 'idle_time': 1,
            'title': 'aa0i1'},
        {'channel': 'a', 'platform': 'b', 'version': 0, 'title': 'ab0'},
        {'channel': 'b', 'platform': 'a', 'version': 2, 'title': 'ba2',
            'dest_url': 'http://example.org'},
        {'channel': 'e', 'platform': None, 'version': None,
            'title': 'utf8',
            'body':  "\u0192\u00a9\u02d9\u02da\u02da\u00ac\u2206\u02d9\u02da\u02d9\u00a9\u2206\u00a9\u0192\u00df \u0376\u0376\u0376\u0376\u0376\u0376\u0376\u0376\u0376\u0376\u0376\u0376\u0376\u0376"}
    ]

    settings = TConfig({'db.type': 'sqlite',
                        'db.db': '/tmp/test.db',
                        'logging.use_heka': False,
                        'db.checkAccount': False})

    def req(self, matchdict={}, user_id=None, headers=None, **kw):

        class Reg(dict):

            settings = {}

            def __init__(self, settings=None, **kw):
                super(Reg, self).__init__(**kw)
                if settings:
                    self.settings = settings

        request = Request(headers=headers, **kw)
        request.GET = kw.get('params', {})
        request.registry = Reg(settings=self.config.get_settings())
        request.registry['storage'] = self.storage
        request.registry['logger'] = self.logger
        request.registry['counter'] = self.counter
        request.registry['auth'] = mock.Mock()
        request.registry['auth'].get_user_id.return_value = user_id
        request.registry.settings.update(self.settings.settings)
        if matchdict:
            request.matchdict.update(matchdict)
        return request

    def setUp(self):
        self.config = testing.setUp()
        self.logger = Logging(self.settings, None)
        self.storage = Storage(config=self.settings, logger=self.logger)
        self.counter = self.storage.counter
        records = []
        for diff in self.diffs:
            record = self.base_record.copy()
            record.update(diff)
            records.append(record)
        self.storage.put_announce(records, now=self.now)
        #time.sleep(3);

    def tearDown(self):
        self.storage.purge()

    def test_get_announcements(self):
        # normal number
        response = views.get_announcements(self.req(matchdict={'channel': 'a',
                                           'platform': 'a', 'version': 0}),
                                           now=self.now)
        eq_(len(json.loads(response.body)['announcements']), 3)
        assert('Date' in response.headers)
        response = views.get_announcements(self.req(matchdict={'channel': 'a',
                                           'platform': 'a', 'version': 0},
                                           headers={'Accept-Language': 'en'}),
                                           now=self.now)
        eq_(len(json.loads(response.body)['announcements']), 3)
        # idle number
        response = views.get_announcements(self.req(matchdict={'channel': 'a',
                              'platform': 'a',
                              'version': 0},
                              params={'idle': '6'}))
        eq_(len(json.loads(response.body)['announcements']), 4)
        timestamp = time.strftime("%a, %d %b %Y %H:%M:%S GMT",
                                  time.gmtime(self.now + 1))
        self.assertRaises(http.HTTPNotModified,
                          views.get_announcements,
                          self.req(matchdict={'channel': 'a',
                                              'platform': 'a', 'version': 0},
                          headers={'If-Modified-Since': timestamp}))
        self.storage.purge()
        self.assertRaises(http.HTTPNoContent,
                          views.get_announcements,
                          self.req(matchdict={'channel': 'z', 'platform': 'z',
                                              'version': 'z'}))

    def test_get_all(self):
        self.assertRaises(http.HTTPUnauthorized,
                          views.get_all_announcements,
                          self.req())
        # scan for include.js or 'test="login"' id?
        # try with a 'valid' user id
        self.assertRaises(http.HTTPUnauthorized,
                          views.get_all_announcements,
                          self.req(matchdict={},
                                   user_id='*****@*****.**'))
        # try successful json
        req = self.req(matchdict={}, user_id='*****@*****.**')
        req.accept_encoding = 'application/javascript'
        response = views.get_all_announcements(req)
        eq_(len(response['announcements']), 7)

    def test_get_lang_loc(self):
        response = views.get_lang_loc(self.req(
                                      headers={'Accept-Language': 'en_US'}))
        eq_(response, {'lang': 'en', 'locale': 'US'})
        response = views.get_lang_loc(self.req(
                                      headers={'Accept-Language': 'en-US'}))
        eq_(response, {'lang': 'en', 'locale': 'US'})
        response = views.get_lang_loc(self.req(
                                      headers={'Accept-Language': 'en'}))
        eq_(response, {'lang': 'en', 'locale': None})

    def test_handle_redir(self):
        # get a record
        response = self.storage.get_announce({'channel': 'b'})
        record = response[0]
        rec_id = record['url'].split('/').pop()
        req = self.req(matchdict={'token': rec_id})
        self.assertRaises(http.HTTPTemporaryRedirect,
                          views.handle_redir, req)
        self.assertRaises(http.HTTPNotFound, views.handle_redir,
                          self.req(matchdict={'token': 'Invalid Token'}))

    def test_admin_page(self):
        req = self.req()
        response = views.admin_page(req)
        eq_(response.status_code, 403)
        req = self.req(matchdict={}, user_id='*****@*****.**')
        response = views.admin_page(req)
        eq_(response.status_code, 200)
        req.registry.settings.update({'auth.block_authoring': True})
        self.assertRaises(http.HTTPNotFound, views.admin_page, req)

    def test_manage_announce(self):
        # test assertion post
        req = self.req(matchdict={'channel': 'c', 'title': 'Goat',
                                  'body': 'Ready for sacrifice'},
                       user_id='*****@*****.**')
        response = views.manage_announce(req)
        # test create
        ann = views.get_announcements(self.req(matchdict={'channel': 'c'}))
        response = json.loads(ann.body)
        goat = None
        for record in response['announcements']:
            if record['title'] == 'Goat':
                goat = record
                break
        goat_id = goat['url'].split('/').pop()
        self.assertIsNotNone(goat)
        req = self.req(params={'delete': goat_id},
                       user_id='*****@*****.**')
        self.assertRaises(http.HTTPOk, views.del_announce, req)
        time.sleep(2)  # Give the db a second to write the record.
        req = self.req(matchdict={'token': goat_id})
        self.assertRaises(http.HTTPNotFound, views.handle_redir, req)