Exemple #1
0
 def test_add_project(self):
     url = '/api/project/'
     response = self.client.post(url, VALID_DATA)
     eq_(response.status_code, 200)
     response_data = json.loads(response.content)
     eq_(response_data['error'], 'okay')
     ok_('_id' in response_data['project'])
     project = Project.objects.get()
     json.loads(project.metadata)
 def test_add_project(self):
     url = '/api/project/'
     response = self.client.post(url, VALID_DATA)
     eq_(response.status_code, 200)
     response_data = json.loads(response.content)
     eq_(response_data['error'], 'okay')
     ok_('_id' in response_data['project'])
     project = Project.objects.get()
     json.loads(project.metadata)
 def test_get_detail_project(self):
     project = create_project(author=self.user, status=Project.LIVE,
                              template=self.template)
     url = '/api/project/%s' % project.uuid
     response = self.client.get(url)
     eq_(response.status_code, 200)
     response_data = json.loads(response.content)
     eq_(response_data['error'], 'okay')
     ok_(isinstance(response_data['project'], basestring))
     json.loads(response_data['project'])
Exemple #4
0
 def test_get_detail_project(self):
     project = create_project(author=self.user,
                              status=Project.LIVE,
                              template=self.template)
     url = '/api/project/%s' % project.uuid
     response = self.client.get(url)
     eq_(response.status_code, 200)
     response_data = json.loads(response.content)
     eq_(response_data['error'], 'okay')
     ok_(isinstance(response_data['project'], basestring))
     json.loads(response_data['project'])
Exemple #5
0
    def test_to_python(self):
        j_field = JSONField()

        self.assertEqual(loads('1'), j_field.to_python('1'))

        self.assertEqual(loads('"1"'), j_field.to_python('"1"'))

        self.assertEqual(loads('[{"a": 1}]'), j_field.to_python('[{"a": 1}]'))

        self.assertEqual(loads('[{"a": "1"}]'),
                         j_field.to_python('[{"a": "1"}]'))
 def test_post_detail_project_forkable(self):
     project = create_project(author=self.other, status=Project.LIVE,
                              is_forkable=True, template=self.template)
     url = '/api/project/%s' % project.uuid
     response = self.client.post(url, VALID_DATA)
     eq_(response.status_code, 200)
     response_data = json.loads(response.content)
     eq_(response_data['error'], 'okay')
     ok_('_id' in response_data['project'])
     json.loads(project.metadata)
     eq_(Project.objects.filter(author=self.user).count(), 1)
     eq_(Project.objects.filter(author=self.other).count(), 1)
Exemple #7
0
 def test_post_detail_project_forkable(self):
     project = create_project(author=self.other,
                              status=Project.LIVE,
                              is_forkable=True,
                              template=self.template)
     url = '/api/project/%s' % project.uuid
     response = self.client.post(url, VALID_DATA)
     eq_(response.status_code, 200)
     response_data = json.loads(response.content)
     eq_(response_data['error'], 'okay')
     ok_('_id' in response_data['project'])
     json.loads(project.metadata)
     eq_(Project.objects.filter(author=self.user).count(), 1)
     eq_(Project.objects.filter(author=self.other).count(), 1)
 def test_whoami(self):
     response = self.client.get('/api/whoami')
     eq_(response.status_code, 200)
     response_data = json.loads(response.content)
     ok_(response_data['username'])
     ok_(response_data['name'])
     ok_(response_data['email'])
Exemple #9
0
 def test_whoami(self):
     response = self.client.get('/api/whoami')
     eq_(response.status_code, 200)
     response_data = json.loads(response.content)
     ok_(response_data['username'])
     ok_(response_data['name'])
     ok_(response_data['email'])
Exemple #10
0
def prepare_project_stream(stream, base_url, metadata):
    """ Sanitizes a butter HTML export
     - Picks the plug-in required from the stream.
    """
    stream = force_unicode(stream) if stream else u""
    tree = treebuilders.getTreeBuilder("lxml")
    parser = html5lib.HTMLParser(tree=tree, namespaceHTMLElements=False)
    document_tree = parser.parse(stream)
    # plugins are relative
    scripts = document_tree.xpath("//script[@src]")
    plugins = [s.get("src") for s in scripts if not urlparse(s.get("src")).netloc]
    # styles are relative
    styles = document_tree.xpath("//link[@href]")
    css = [s.get("href") for s in styles if not urlparse(s.get("href")).netloc]
    # inline css
    inline_css = []
    for inline in document_tree.xpath("//style"):
        inline_css.append(strip_tags(inline.text))
        inline.getparent().remove(inline)
    # remove script tags
    for inline in document_tree.xpath("//script"):
        inline.getparent().remove(inline)
    popcorn = prepare_popcorn_string_from_project_data(json.loads(metadata)) if metadata else ""
    body = [clean(tostring(b)) + popcorn for b in document_tree.xpath("//body")]
    context = {"styles": css, "scripts": plugins, "inline_css": inline_css, "body": body}

    return render_to_string("project/skeleton.html", context)
Exemple #11
0
 def test_project_detail(self):
     project = create_project(author=self.user, status=Project.LIVE)
     url = self.get_url('user_project_meta', self.user, project)
     response = self.client.get(url)
     eq_(response.status_code, 200)
     data = json.loads(response.content)
     eq_(data['project'], project.name)
Exemple #12
0
 def test_prepare_popcorn_string(self):
     metadata = json.loads(POPCORN_METADATA)
     result = prepare_popcorn_string_from_project_data(metadata)
     spaceless_result = re.sub(r'\s', '', result)
     ok_('(function(){varpopcorn=Popcorn.smart("#' +
         metadata['media'][0]['target'] + '","' +
         metadata['media'][0]['url'] + '"' in spaceless_result)
 def test_project_detail(self):
     project = create_project(author=self.user, status=Project.LIVE)
     url = self.get_url('user_project_meta', self.user, project)
     response = self.client.get(url)
     eq_(response.status_code, 200)
     data = json.loads(response.content)
     eq_(data['project'], project.name)
 def test_list_project_removed(self):
     create_project(author=self.user, status=Project.REMOVED,
                    template=self.template)
     response = self.client.get('/api/projects')
     eq_(response.status_code, 200)
     response_data = json.loads(response.content)
     eq_(response_data['error'], 'okay')
     eq_(len(response_data['projects']), 0)
 def test_publish_project(self):
     project = create_project(author=self.user, template=self.template)
     url = '/api/publish/%s' % project.uuid
     response = self.client.post(url, {})
     eq_(response.status_code, 200)
     response_data = json.loads(response.content)
     eq_(response_data['error'], 'okay')
     ok_('url' in response_data)
 def test_post_detail_project_invalid(self):
     project = create_project(author=self.user, template=self.template)
     url = '/api/project/%s' % project.uuid
     response = self.client.post(url, {'template': 'invalid'})
     eq_(response.status_code, 200)
     response_data = json.loads(response.content)
     eq_(response_data['error'], 'error')
     ok_('form_errors' in response_data)
Exemple #17
0
 def test_publish_project(self):
     project = create_project(author=self.user, template=self.template)
     url = '/api/publish/%s' % project.uuid
     response = self.client.post(url, {})
     eq_(response.status_code, 200)
     response_data = json.loads(response.content)
     eq_(response_data['error'], 'okay')
     ok_('url' in response_data)
Exemple #18
0
 def test_post_detail_project_invalid(self):
     project = create_project(author=self.user, template=self.template)
     url = '/api/project/%s' % project.uuid
     response = self.client.post(url, {'template': 'invalid'})
     eq_(response.status_code, 200)
     response_data = json.loads(response.content)
     eq_(response_data['error'], 'error')
     ok_('form_errors' in response_data)
Exemple #19
0
 def test_post_detail_project(self):
     project = create_project(author=self.user, template=self.template)
     url = '/api/project/%s' % project.uuid
     response = self.client.post(url, VALID_DATA)
     eq_(response.status_code, 200)
     response_data = json.loads(response.content)
     eq_(response_data['error'], 'okay')
     ok_('_id', response_data['project'])
     ok_('data', response_data['project'])
Exemple #20
0
 def test_list_project_removed(self):
     create_project(author=self.user,
                    status=Project.REMOVED,
                    template=self.template)
     response = self.client.get('/api/projects')
     eq_(response.status_code, 200)
     response_data = json.loads(response.content)
     eq_(response_data['error'], 'okay')
     eq_(len(response_data['projects']), 0)
Exemple #21
0
 def test_list_projects(self):
     alex = create_user('alex')
     create_project(author=alex, template=self.template)
     create_project(author=self.user, template=self.template)
     response = self.client.get('/api/projects')
     eq_(response.status_code, 200)
     response_data = json.loads(response.content)
     eq_(response_data['error'], 'okay')
     eq_(len(response_data['projects']), 1)
Exemple #22
0
def user_project_data(request, project):
    response = project.metadata if project.metadata else "{}"
    # If user is the owner save automatically else, force new name
    if request.user.is_authenticated() and request.user == project.author:
        json_response = json.loads(response)
        json_response['projectID'] = project.uuid
        json_response['name'] = project.name
        response = json.dumps(json_response)
    return HttpResponse(response, mimetype='application/json')
 def test_template_config(self):
     template = create_template(status=Template.LIVE)
     response = self.client.get(reverse('template_config',
                                        args=[template.slug]))
     eq_(response.status_code, 200)
     response = json.loads(response.content)
     ok_('savedDataUrl' in response)
     ok_('baseDir' in response)
     ok_('name' in response)
Exemple #24
0
 def test_template_config(self):
     template = create_template(status=Template.LIVE)
     response = self.client.get(
         reverse('template_config', args=[template.slug]))
     eq_(response.status_code, 200)
     response = json.loads(response.content)
     ok_('savedDataUrl' in response)
     ok_('baseDir' in response)
     ok_('name' in response)
Exemple #25
0
 def test_project_detail(self):
     project = create_project(author=self.user, status=Project.LIVE)
     url = self.get_url('user_project_data', self.user, project)
     response = self.client.get(url)
     eq_(response.status_code, 200)
     data = json.loads(response.content)
     ok_('data' in data)
     ok_(not 'projectID' in data)
     ok_(not 'name' in data)
Exemple #26
0
def user_project_data(request, project):
    response = project.metadata if project.metadata else "{}"
    # If user is the owner save automatically else, force new name
    if request.user.is_authenticated() and request.user == project.author:
        json_response = json.loads(response)
        json_response['projectID'] = project.uuid
        json_response['name'] = project.name
        response = json.dumps(json_response)
    return HttpResponse(response, mimetype='application/json')
 def test_list_projects(self):
     alex = create_user('alex')
     create_project(author=alex, template=self.template)
     create_project(author=self.user, template=self.template)
     response = self.client.get('/api/projects')
     eq_(response.status_code, 200)
     response_data = json.loads(response.content)
     eq_(response_data['error'], 'okay')
     eq_(len(response_data['projects']), 1)
 def test_unpublished_project_owner(self):
     project = create_project(author=self.user, status=Project.HIDDEN)
     url = self.get_url('user_project_meta', self.user, project)
     self.client.login(username=self.user.username, password='******')
     response = self.client.get(url)
     eq_(response.status_code, 200)
     data = json.loads(response.content)
     eq_(data['project'], project.name)
     self.client.logout()
Exemple #29
0
 def test_unpublished_project_owner(self):
     project = create_project(author=self.user, status=Project.HIDDEN)
     url = self.get_url('user_project_meta', self.user, project)
     self.client.login(username=self.user.username, password='******')
     response = self.client.get(url)
     eq_(response.status_code, 200)
     data = json.loads(response.content)
     eq_(data['project'], project.name)
     self.client.logout()
 def test_project_detail(self):
     project = create_project(author=self.user, status=Project.LIVE)
     url = self.get_url('user_project_data', self.user, project)
     response = self.client.get(url)
     eq_(response.status_code, 200)
     data = json.loads(response.content)
     ok_('data' in data)
     ok_(not 'projectID' in data)
     ok_(not 'name' in data)
 def test_post_detail_project(self):
     project = create_project(author=self.user,
                              template=self.template)
     url = '/api/project/%s' % project.uuid
     response = self.client.post(url, VALID_DATA)
     eq_(response.status_code, 200)
     response_data = json.loads(response.content)
     eq_(response_data['error'], 'okay')
     ok_('_id', response_data['project'])
     ok_('data', response_data['project'])
Exemple #32
0
def _add_popcorn_metadata(document_tree, metadata):
    """Transform the metadata into Popcorn instructions"""
    if not metadata:
        return document_tree
    data = json.loads(metadata)
    popcorn = prepare_popcorn_string_from_project_data(data)
    body = document_tree.xpath('//body')[0]
    script = E.SCRIPT(popcorn, type="text/javascript")
    body.append(script)
    return document_tree
Exemple #33
0
def _add_popcorn_metadata(document_tree, metadata):
    """Transform the metadata into Popcorn instructions"""
    if not metadata:
        return document_tree
    data = json.loads(metadata)
    popcorn = prepare_popcorn_string_from_project_data(data)
    body = document_tree.xpath('//body')[0]
    script = E.SCRIPT(popcorn, type="text/javascript")
    body.append(script)
    return document_tree
Exemple #34
0
 def wrapper(request, *args, **kwargs):
     request.JSON = {}
     content_type = request.META.get('CONTENT_TYPE', 'text/plain')
     request.is_json = True if 'application/json' in content_type else False
     if (request.method == 'POST' and request.is_json):
         try:
             data = json.loads(request.raw_post_data)
         except ValueError:
             return HttpResponseBadRequest()
         request.JSON = data
     return func(request, *args, **kwargs)
Exemple #35
0
 def wrapper(request, *args, **kwargs):
     request.JSON = {}
     content_type = request.META.get('CONTENT_TYPE', 'text/plain')
     request.is_json = True if 'application/json' in content_type else False
     if (request.method == 'POST' and request.is_json):
         try:
             data = json.loads(request.raw_post_data)
         except ValueError:
             return HttpResponseBadRequest()
         request.JSON = data
     return func(request, *args, **kwargs)
Exemple #36
0
    def test_update_times(self):
        Client().post('/add_programmer/', {'programmer_name':'susan'})
        Client().post('/add_programmer/', {'programmer_name':'aimee'})

        old_times = get_pair_times(first = 'susan', second = 'aimee')

        response = Client().get("/update_times/susan_aimee")

        self.assertEqual(old_times + 1, get_pair_times(first='susan', second='aimee'))

        json_data = loads(response.content)
        self.assertEqual(json_data['result'], "success")
 def test_published_project_not_owner(self):
     other = create_user('other')
     project = create_project(author=other, status=Project.LIVE)
     url = self.get_url('user_project_data', other, project)
     self.client.login(username=self.user.username, password='******')
     response = self.client.get(url)
     eq_(response.status_code, 200)
     data = json.loads(response.content)
     ok_('data' in data)
     ok_(not 'projectID' in data)
     ok_(not 'name' in data)
     self.client.logout()
Exemple #38
0
 def test_published_project_not_owner(self):
     other = create_user('other')
     project = create_project(author=other, status=Project.LIVE)
     url = self.get_url('user_project_data', other, project)
     self.client.login(username=self.user.username, password='******')
     response = self.client.get(url)
     eq_(response.status_code, 200)
     data = json.loads(response.content)
     ok_('data' in data)
     ok_(not 'projectID' in data)
     ok_(not 'name' in data)
     self.client.logout()
    def test_to_python(self):
        j_field = JSONField()

        self.assertEqual(
            loads('1'),
            j_field.to_python('1')
        )

        self.assertEqual(
            loads('"1"'),
            j_field.to_python('"1"')
        )

        self.assertEqual(
            loads('[{"a": 1}]'),
            j_field.to_python('[{"a": 1}]')
        )

        self.assertEqual(
            loads('[{"a": "1"}]'),
            j_field.to_python('[{"a": "1"}]')
        )
Exemple #40
0
    def test_add_references__negative(self, mock_put):
        colResponse = MagicMock(spec=Response)
        mock_put.return_value = colResponse
        colResponse.json.return_value = {'/foobar/': ['Reference not valid']}
        colResponse.status_code = 400

        collectionAddReferenceView = views.CollectionAddReferenceView()
        collectionAddReferenceView.kwargs = {}
        request = FakeRequest()
        request.body = json.dumps({'expressions': []})
        collectionAddReferenceView.request = request
        response = json.loads(collectionAddReferenceView.post(request).content)
        self.assertEquals(response['errors'], colResponse.json.return_value)
Exemple #41
0
    def test_add_references__bad_request(self, mock_put):
        colResponse = MagicMock(spec=Response)
        mock_put.return_value = colResponse
        colResponse.status_code = 400

        collectionAddReferenceView = views.CollectionAddReferenceView()
        collectionAddReferenceView.kwargs = {
            'collection': '1',
        }

        request = FakeRequest()
        request.body = json.dumps({'expressions': []})
        collectionAddReferenceView.request = request
        response = json.loads(collectionAddReferenceView.post(request).content)
        self.assertEquals(response['errors'], EXPRESSIONS_SHOULD_EXIST)
Exemple #42
0
    def test_add_references__positive(self, mock_put):
        colResponse = MagicMock(spec=Response)
        mock_put.return_value = colResponse
        colResponse.status_code = 200

        collectionAddReferenceView = views.CollectionAddReferenceView()
        collectionAddReferenceView.kwargs = {
            'collection': '1',
        }
        request = FakeRequest()
        request.body = json.dumps({'expressions': []})
        collectionAddReferenceView.request = request
        response = json.loads(collectionAddReferenceView.post(request).content)
        self.assertEquals(response['errors'], [])
        self.assertEquals(response['success_url'],
                          '/users/tempuser/collections/1/references/')
Exemple #43
0
    def test_add_references__bad_request(self, mock_put):
        colResponse = MagicMock(spec=Response)
        mock_put.return_value = colResponse
        colResponse.status_code = 400

        collectionAddReferenceView = views.CollectionAddReferenceView()
        collectionAddReferenceView.kwargs = {
            'collection': '1',
        }

        request = FakeRequest()
        request.body = json.dumps({
            'expressions': []
        })
        collectionAddReferenceView.request = request
        response = json.loads(
            collectionAddReferenceView.post(request).content
        )
        self.assertEquals(response['errors'], EXPRESSIONS_SHOULD_EXIST)
Exemple #44
0
    def test_add_references__negative(self, mock_put):
        colResponse = MagicMock(spec=Response)
        mock_put.return_value = colResponse
        colResponse.json.return_value = {
            '/foobar/': ['Reference not valid']
        }
        colResponse.status_code = 400

        collectionAddReferenceView = views.CollectionAddReferenceView()
        collectionAddReferenceView.kwargs = {}
        request = FakeRequest()
        request.body = json.dumps({
            'expressions': []
        })
        collectionAddReferenceView.request = request
        response = json.loads(
            collectionAddReferenceView.post(request).content
        )
        self.assertEquals(response['errors'], colResponse.json.return_value)
Exemple #45
0
    def test_add_references__positive(self, mock_put):
        colResponse = MagicMock(spec=Response)
        mock_put.return_value = colResponse
        colResponse.status_code = 200

        collectionAddReferenceView = views.CollectionAddReferenceView()
        collectionAddReferenceView.kwargs = {
            'collection': '1',
        }
        request = FakeRequest()
        request.body = json.dumps({
            'expressions': []
        })
        collectionAddReferenceView.request = request
        response = json.loads(
            collectionAddReferenceView.post(request).content
        )
        self.assertEquals(response['errors'], [])
        self.assertEquals(
            response['success_url'],
            '/users/tempuser/collections/1/references/'
        )
Exemple #46
0
def prepare_config_stream(stream, base_url):
    """Prepares the config to be stored in the database"""
    data = json.loads(stream)
    data = _remove_default_values(data)
    return json.dumps(data)
Exemple #47
0
 def test_add_popcorn_plugins(self):
     config = json.loads(POPCORN_CONFIG)
     document_tree = _get_document_tree('<html><head></head></html>')
     _add_popcorn_plugins(document_tree, config)
     eq_(len(document_tree.xpath('//script[@src]')), 4)
Exemple #48
0
def prepare_config_stream(stream, base_url):
    """Prepares the config to be stored in the database"""
    data = json.loads(stream)
    data = _remove_default_values(data)
    return json.dumps(data)
Exemple #49
0
def remove_default_values(stream, base_url=None):
    data = json.loads(stream)
    for attr in ['baseDir', 'name', 'savedDataUrl']:
        if attr in data:
            del data[attr]
    return json.dumps(data)
 def test_add_popcorn_plugins(self):
     config = json.loads(POPCORN_CONFIG)
     document_tree = _get_document_tree('<html><head></head></html>')
     _add_popcorn_plugins(document_tree, config)
     eq_(len(document_tree.xpath('//script[@src]')), 4)
 def test_prepare_popcorn_string(self):
     metadata = json.loads(POPCORN_METADATA)
     result = prepare_popcorn_string_from_project_data(metadata)
     spaceless_result = re.sub(r'\s', '', result)
     ok_('(function(){varpopcorn=Popcorn.smart("#' + metadata['media'][0]['target'] + '","' + metadata['media'][0]['url'] + '"' in spaceless_result)
Exemple #52
0
def remove_default_values(stream, base_url=None):
    data = json.loads(stream)
    for attr in ['baseDir', 'name', 'savedDataUrl']:
        if attr in data:
            del data[attr]
    return json.dumps(data)
Exemple #53
0
def remove_default_values(stream, base_url=None):
    data = json.loads(stream)
    for attr in ["baseDir", "name", "savedDataUrl"]:
        if attr in data:
            del data[attr]
    return json.dumps(data)