Ejemplo n.º 1
0
    def test_treat_errors_on_netprint_sync(self):
        user = create_user()
        file_info = create_file_info(user, path=u'/A4/テスト.doc')

        service = self._getOUT(user)

        class dropbox(object):
            @staticmethod
            def list(path, recursive=None):
                root_dir = app_dir()
                for dir in root_dir['contents']:
                    if dir['path'] == u'/A4':
                        a4_dir = dir
                        break
                else:
                    self.fail('No A4 directory')
                a4_dir['contents'].append(
                        create_dropbox_item(
                            path=file_info.path))
                return root_dir

        class netprint(object):
            @staticmethod
            def list():
                return [create_netprint_item(
                            name=file_info.as_netprint_name(True),
                            error=True)]

        service.dropbox = dropbox
        service.netprint = netprint
        service.sync()
Ejemplo n.º 2
0
    def test_do_not_make_report_if_no_change(self):
        import hashlib
        from google.appengine.api import memcache
        from netprintbox.data import FileState
        from netprintbox.utils import (
            normalize_name, get_namespace)

        NETPRINT_ID = 'latest'
        ORIGINAL_PATH = '/latest.data'
        NETPRINT_NAME = normalize_name(ORIGINAL_PATH)

        user = create_user()
        create_file_info(user,
                         path=ORIGINAL_PATH,
                         netprint_id=NETPRINT_ID,
                         netprint_name=NETPRINT_NAME,
                         state=FileState.LATEST)

        class netprint(object):
            @staticmethod
            def list():
                return [
                        create_netprint_item(
                            id=NETPRINT_ID,
                            name=NETPRINT_NAME)
                       ]

        md5 = hashlib.new('md5')
        md5.update(NETPRINT_ID)
        memcache.set(str(user.key()), md5.hexdigest(),
                     namespace=get_namespace())

        service = self._getOUT(user)
        service.netprint = netprint

        (need_report, _result) = service._make_report()
        self.assertFalse(need_report)

        # check no exception is occurred.
        service.make_report()
Ejemplo n.º 3
0
    def test_it(self):
        from netprintbox.data import DropboxFileInfo
        from netprintbox.views import pin as pin_view

        token = 'a_token'
        user = create_user()
        file_info = create_file_info(user)

        def post(key, pin, expected):
            data = {'file_key': key, 'pin': pin,
                    'token': token}

            class session(object):
                def get_csrf_token(self):
                    return token

                def __getitem__(self, key):
                    if key == 'netprintbox.dropbox_user.key':
                        return str(user.key())
                    else:
                        raise KeyError

            request = testing.DummyRequest(
                    headers={'Content-Type': 'application/json'},
                    body=json.dumps(data))
            request.session = session()
            response = pin_view(request)
            self.assertEqual(response.status_int, 200)
            result = json.loads(response.body)
            self.assertEqual(result['pin'], expected)

            modified_file_info = DropboxFileInfo.get(key)
            self.assertEqual(modified_file_info.pin, expected)

        file_key = str(file_info.key())
        post(file_key, 'on', True)
        post(file_key, 'off', False)
Ejemplo n.º 4
0
    def test_treat_errors_on_netprint_making_report(self):
        user = create_user()
        file_info = create_file_info(user, path=u'/A4/テスト.doc')

        service = self._getOUT(user)

        class dropbox(object):
            @staticmethod
            def list(path, recursive=None):
                root_dir = app_dir()
                for dir in root_dir['contents']:
                    if dir['path'] == u'/A4':
                        a4_dir = dir
                        break
                else:
                    self.fail('No A4 directory')
                a4_dir['contents'].append(
                        create_dropbox_item(
                            path=file_info.path))
                return root_dir

        class netprint(object):
            @staticmethod
            def list():
                return [create_netprint_item(
                            name=file_info.as_netprint_name(True),
                            error=True)]

        service.dropbox = dropbox
        service.netprint = netprint
        (need_report, result) = service._make_report()
        self.assertTrue(need_report)
        self.assertEqual(len(result), 1)
        item = result[0]
        self.assertTrue(item['error'])
        self.assertTrue(item['controlled'])
Ejemplo n.º 5
0
    def test_make_report(self):
        from google.appengine.api import memcache
        from netprintbox.settings import DATETIME_FORMAT, REPORT_PATH
        from netprintbox.data import FileState
        from netprintbox.utils import normalize_name

        user = create_user()
        f1_path = '/need_netprint_id.data'
        f1 = create_file_info(user,
                              path=f1_path,
                              netprint_name=normalize_name(f1_path),
                              state=FileState.NEED_NETPRINT_ID)
        f2_path = '/lost_by_something.data'
        f2 = create_file_info(user,
                              path=f2_path,
                              netprint_name=normalize_name(f2_path),
                              state=FileState.LATEST)
        f3_path = '/latest.data'
        f3 = create_file_info(user,
                              path=f3_path,
                              netprint_id='latest',
                              netprint_name=normalize_name(f3_path),
                              state=FileState.LATEST)

        request = testing.DummyRequest()
        self.setUpPyramid(request=request)

        class netprint(object):
            @staticmethod
            def list():
                return [
                        create_netprint_item(
                            id='uncontrolled',
                            name='uncontrolled'),
                        create_netprint_item(
                            id='latest',
                            name=normalize_name('/latest.data')),
                       ]

        put_result = []

        class dropbox(object):
            @staticmethod
            def put(path, file_obj):
                put_result.append((path, file_obj))

        service = self._getOUT(user)
        service.netprint = netprint
        service.dropbox = dropbox

        (need_report, result) = service._make_report()
        self.assertTrue(need_report)
        self.assertItemsEqual(
            [(item['id'], item['name'], item.get('controlled', False),
              item['last_modified']) for item in result],
            [('FAKE:WAIT', f1.as_netprint_name(), True,
              f1.local_last_modified.strftime(DATETIME_FORMAT)),
             ('FAKE:ERROR', f2.as_netprint_name(), True,
              f2.local_last_modified.strftime(DATETIME_FORMAT)),
             ('uncontrolled', 'uncontrolled', False, None),
             ('latest', f3.as_netprint_name(), True,
              f3.local_last_modified.strftime(DATETIME_FORMAT)),
            ])

        # flush side-effect of above.
        memcache.flush_all()

        service.make_report()
        self.assertEqual(len(put_result), 1)
        self.assertEqual(put_result[0][0], REPORT_PATH)
        report_data = put_result[0][1].read()
        if False:
            with open('report.html', 'wb') as f:
                f.write(report_data)
        self.assertRegexpMatches(report_data, 'latest')
        self.assertRegexpMatches(report_data, 'uncontrolled')
Ejemplo n.º 6
0
 def test_own_files(self):
     user = self._getOUT()
     create_file_info(user)
     create_file_info(user)
     create_file_info(user)
     self.assertEqual(user.own_files().count(), 3)