web/ldt/text/tests.py
author wakimd
Fri, 19 Nov 2010 18:12:57 +0100
changeset 19 7cf81d58a968
parent 17 683ce4109c28
child 21 1a061f244254
permissions -rw-r--r--
oauth start
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
     1
#encoding:UTF-8
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
     2
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
     3
""" Run these tests with 'python manage.py test text'  """
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
     4
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
     5
from django.test import TestCase
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
     6
import unittest
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
     7
import lxml.etree
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
     8
from ldt.text.models import *
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
     9
from ldt.core.models import Owner
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    10
from views import *
16
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 10
diff changeset
    11
import urllib
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    12
import uuid
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    13
import tempfile
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    14
import datetime
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    15
from django.contrib.auth.models import *
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    16
from django.conf import settings
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    17
from django.test.client import Client
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    18
from ldt.text import VERSION_STR
17
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    19
from django.db import transaction
19
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
    20
from django.contrib.auth.models import User
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
    21
from oauth_provider.models import Resource, Consumer
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
    22
import time
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
    23
from oauth_provider.models import Token
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
    24
from oauth.oauth import OAuthRequest, OAuthSignatureMethod_HMAC_SHA1
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
    25
from django.contrib.auth.models import User
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
    26
from oauth_provider.models import Resource, Consumer, Token, Nonce
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
    27
import time
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
    28
from oauth_provider.consts import OUT_OF_BAND
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
    29
from oauth.oauth import OAuthRequest, OAuthSignatureMethod_PLAINTEXT, generate_nonce
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    30
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    31
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    32
# This test creates an annotation and checks that:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    33
# 1. the annotation was created in the database (by trying to access it through a 'get')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    34
# 2. the returned xml contains correct data
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    35
class CreateTest(unittest.TestCase):
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    36
    def setUp(self):
16
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 10
diff changeset
    37
        self.content = str('<iri><text-annotation><id>f2c1d1fa-629d-4520-a3d2-955b4f2582c0</id><uri>http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168</uri><tags><tag>tag1</tag><tag>tag2</tag></tags><content><color>#AAAAAA</color><description><![CDATA[texte de description]]></description><title><![CDATA[titre de l\'annotation]]></title><text><![CDATA[texte selectionne lors de la creation de l\'annotation]]></text></content><meta><contributor>oaubert</contributor><contributor-id>79cd0532-1dda-4130-b351-6a181130a7c9</contributor-id><created>2010-09-06 12:33:53.417550</created><creator>oaubert</creator><creator-id>79cd0532-1dda-4130-b351-6a181130a7c9</creator-id><modified>2010-09-06 12:33:53.420459</modified></meta></text-annotation></iri>')
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    38
        self.c = Client()
17
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    39
        self.annot = Annotation(external_id=u'd2c1d1fa-629d-4520-a3d2-955b4f2582c0', uri=u'http://iri.blabla', tags=u"tag1,tag2", title=u'montitre', description=u'madesc', text=u'letexteselectionne', color=u'#AAAAAA', creator=u'wakimd', contributor=u'wakimd', creation_date=u'2010-09-06 12:33:53.417550', update_date=u'2010-09-06 12:33:53.417550')
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    40
        self.annot.save()
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    41
    def tearDown(self):
17
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    42
        transaction.rollback()
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    43
        annotlist=Annotation.objects.all()
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    44
        for annot in annotlist:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    45
            annot.delete()
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    46
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    47
    def test_create_annotation(self):
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    48
        response = self.c.post('/api/'+ VERSION_STR +'/text/create/', {'content':self.content})
16
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 10
diff changeset
    49
        #self.assertEqual(response.content, "  ")
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    50
        self.annot1 = lxml.etree.fromstring(response.content)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    51
        self.assertEqual(self.annot1.xpath("/iri/text-annotation/id/text()")[0],"f2c1d1fa-629d-4520-a3d2-955b4f2582c0")
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    52
        self.assertEqual(self.annot1.xpath("/iri/text-annotation/content")[0].tag,"content")
17
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    53
        self.assertEqual(self.annot1.xpath("/iri/text-annotation/tags/tag/text()")[0],u"tag1")
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    54
        self.assertEqual(self.annot1.xpath("/iri/text-annotation/content/text/text()")[0],u"texte selectionne lors de la creation de l\'annotation")
17
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    55
        #self.assertEqual(self.annot1.xpath("/iri/text-annotation/meta/created/text()")[0],"2010-09-06 12:33:53.417550")
16
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 10
diff changeset
    56
        response2 = self.c.get('/api/'+ VERSION_STR +'/text/get/', {'id':'f2c1d1fa-629d-4520-a3d2-955b4f2582c0'})
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 10
diff changeset
    57
        annot2 = lxml.etree.fromstring(response.content)
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 10
diff changeset
    58
        self.assertEqual(annot2.xpath("/iri/text-annotation/uri/text()")[0], "http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168")
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    59
        
16
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 10
diff changeset
    60
    def test_error_create(self):
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 10
diff changeset
    61
        content = '<iri><text-annotation><id>d2c1d1fa-629d-4520-a3d2-955b4f2582c0</id><uri>http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168</uri><tags><tag>tag1</tag><tag>tag2</tag></tags><content><color>#AAAAAA</color><description><![CDATA[texte de description]]></description><title><![CDATA[titre de l\'annotation]]></title><text><![CDATA[texte selectionne lors de la creation de l\'annotation]]></text></content><meta><contributor>oaubert</contributor><contributor-id>79cd0532-1dda-4130-b351-6a181130a7c9</contributor-id><created>2010-09-06 12:33:53.417550</created><creator>oaubert</creator><creator-id>79cd0532-1dda-4130-b351-6a181130a7c9</creator-id><modified>2010-09-06 12:33:53.420459</modified></meta></text-annotation></iri>'
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 10
diff changeset
    62
        response = self.c.post('/api/'+ VERSION_STR +'/text/create/', {'content':content})
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 10
diff changeset
    63
        self.assertEqual(response.status_code, 409)
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    64
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    65
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    66
# This test creates an annotation, then gets it, and checks that the returned xml contains correct data
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    67
class GetTest(unittest.TestCase):
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    68
    def setUp(self):
17
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    69
        self.annotation = Annotation(external_id="d2c1d1fa-629d-4520-a3d2-955b4f2582c0", tags=u"tag1 ,tag2 ,     tag3", title="titre de l\'annotation",text="texte selectionne lors de la creation de l\'annotation",color="#AAAAAA", creation_date="2010-09-06 12:33:53.417550", update_date="2010-09-06 12:33:53.420459")
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    70
        self.annotation.save()
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    71
        self.c = Client()
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    72
    def tearDown(self):
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    73
        annotlist=Annotation.objects.all()
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    74
        for annot in annotlist:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    75
            annot.delete()
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    76
      
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    77
    def test_get_annotation(self):
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    78
        response = self.c.get('/api/'+ VERSION_STR +'/text/get/', {'id':'d2c1d1fa-629d-4520-a3d2-955b4f2582c0'})
17
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    79
        print response
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    80
        self.annot1 = lxml.etree.fromstring(response.content)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    81
        self.assertEqual(self.annot1.xpath("/iri/text-annotation/id/text()")[0],self.annotation.external_id)
17
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    82
        self.assertEqual(self.annot1.xpath("/iri/text-annotation/tags/tag/text()")[1], "tag2")
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    83
        self.assertEqual(self.annot1.xpath("/iri/text-annotation/content/color/text()")[0],self.annotation.color)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    84
        self.assertEqual(self.annot1.xpath("/iri/text-annotation/meta/created/text()")[0], str(self.annotation.creation_date))
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    85
10
000f3ca19eaa Added 404 templates + some views corrections
wakimd
parents: 9
diff changeset
    86
    def test_error_get(self):
000f3ca19eaa Added 404 templates + some views corrections
wakimd
parents: 9
diff changeset
    87
        response = self.c.get('/api/'+ VERSION_STR +'/text/get/', {'id':'2'})
000f3ca19eaa Added 404 templates + some views corrections
wakimd
parents: 9
diff changeset
    88
        self.assertEqual(response.status_code,404)
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    89
        
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    90
        
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    91
class FilterTest(unittest.TestCase):
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    92
    def setUp(self):
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    93
        self.annotation = Annotation(external_id="k2c1d1fa-629d-4520-a3d2-955b4f2582c0",title="titre de l\'annotation",text="texte selectionne lors de la creation de l\'annotation",color="#AAAAAA", uri="http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168", creator="wakimd")
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    94
        self.annotation.save()
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    95
        self.annotation2 = Annotation(external_id="l2c1d1fa-629d-4520-a3d2-955b4f2582c0",title="titre de l\'annotation2",text="texte selectionne lors de la creation de l\'annotation2",color="#BBBBBB", uri="http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168", creator="wakimd")
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    96
        self.annotation2.save()
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    97
        self.annotation3 = Annotation(external_id="m2c1d1fa-629d-4520-a3d2-955b4f2582c0", title="titre3", text="texte3", color="#CCCCCC", uri="http://blabla", creator="wakimd")
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    98
        self.annotation3.save() 
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    99
        self.c = Client()       
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   100
    def tearDown(self):
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   101
        annotlist=Annotation.objects.all()
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   102
        for annot in annotlist:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   103
            annot.delete()
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   104
        
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   105
    def test_filter_annotation_creator_limit(self):
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   106
        user = 'wakimd'
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   107
        uri = "http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168"
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   108
        limit= 1
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   109
        response = self.c.get('/api/'+ VERSION_STR +'/text/filter/', {'uri':uri,'creator':user,'limit':limit})
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   110
        doc = lxml.etree.fromstring(response.content)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   111
        cpt = 0
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   112
        for elem in doc.xpath("/iri/text-annotation"):
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   113
            cpt = cpt + 1
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   114
        if limit is not None:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   115
            self.assertEqual(cpt,limit)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   116
        for elem in doc.xpath("/iri/text-annotation/meta/creator/text()"):
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   117
            self.assertEqual(elem,user)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   118
        for elem in doc.xpath("/iri/text-annotation/uri/text()"):
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   119
            self.assertEqual(elem[:57],"http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml")
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   120
        
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   121
    def test_filter_annotation_uri(self):
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   122
        uri = "http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168"
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   123
        response = self.c.get('/api/'+ VERSION_STR +'/text/filter/', {'uri':uri})
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   124
        doc = lxml.etree.fromstring(response.content)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   125
        for elem in doc.xpath("/iri/text-annotation/uri/text()"):
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   126
            self.assertEqual(elem[:57],"http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml")
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   127
            
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   128
    def test_filter_annotation_filter(self):
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   129
        uri = "http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168"
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   130
        filter = 'lors'
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   131
        limit = None
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   132
        response = self.c.get('/api/'+ VERSION_STR +'/text/filter/', {'uri':uri,'filter':'lors'})
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   133
        doc = lxml.etree.fromstring(response.content)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   134
        for elem in doc.xpath("/iri/text-annotation/content/text/text()"):
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   135
            self.assertTrue('lors' in elem)  
17
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   136
        #for elem in doc.xpath("/iri/text-annotation/meta/creator/text()"):
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   137
        #    self.assertEqual(elem,user)          
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   138
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   139
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   140
# This test creates an annotation, then deletes it, and checks that:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   141
# 1. the annotation doesn't exist anymore in the database (by trying to access it through a 'get')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   142
# 2. the returned xml contains no data
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   143
class DeleteTest(unittest.TestCase):
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   144
    def setUp(self):
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   145
        self.annotation = Annotation(external_id="d2c1d1fa-629d-4520-a3d2-955b4f2582c0",title="titre de l\'annotation",text="texte selectionne lors de la creation de l\'annotation",color="#AAAAAA", creation_date="2010-09-06T12:33:53.417550", update_date="2010-09-06T12:33:53.420459")
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   146
        self.annotation.save()
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   147
        self.c = Client()    
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   148
    def tearDown(self):
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   149
        annotlist=Annotation.objects.all()
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   150
        for annot in annotlist:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   151
            annot.delete()
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   152
    
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   153
    def test_delete_annotation(self):
16
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 10
diff changeset
   154
        id = urllib.urlencode({'id':'d2c1d1fa-629d-4520-a3d2-955b4f2582c0'})
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 10
diff changeset
   155
        f = urllib.urlopen("http://127.0.0.1:8000/api/1.0/text/delete/", id)
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   156
        response = self.c.post('/api/'+ VERSION_STR +'/text/delete/', {'id':'d2c1d1fa-629d-4520-a3d2-955b4f2582c0'})
10
000f3ca19eaa Added 404 templates + some views corrections
wakimd
parents: 9
diff changeset
   157
        response2 = self.c.get('/ldt/get/', {'id':'d2c1d1fa-629d-4520-a3d2-955b4f2582c0'})
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   158
        doc = lxml.etree.fromstring(response.content)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   159
        self.assertEqual(doc.xpath("/iri/text-annotation/id/text()"),[])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   160
        self.assertEqual(doc.xpath("/iri/text-annotation/tags/tag/text()"), [])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   161
        self.assertEqual(doc.xpath("/iri/text-annotation/content/color/text()"),[])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   162
        self.assertEqual(doc.xpath("/iri/text-annotation/meta/creator/text()"),[])
10
000f3ca19eaa Added 404 templates + some views corrections
wakimd
parents: 9
diff changeset
   163
        self.assertEqual(response2.status_code, 404)   
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   164
10
000f3ca19eaa Added 404 templates + some views corrections
wakimd
parents: 9
diff changeset
   165
    def test_error_delete(self):
000f3ca19eaa Added 404 templates + some views corrections
wakimd
parents: 9
diff changeset
   166
        response = self.c.post('/api/'+ VERSION_STR +'/text/ldt/delete/', {'id':'1'})
000f3ca19eaa Added 404 templates + some views corrections
wakimd
parents: 9
diff changeset
   167
        self.assertEqual(response.status_code,404)
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   168
        
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   169
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   170
# This test creates an annotation, then updates it with new content, and checks that the returned xml contains the updated data
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   171
class UpdateTest(unittest.TestCase):
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   172
    def setUp(self):
17
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   173
        self.annotation = Annotation(external_id="d2c1d1fa-629d-4520-a3d2-955b4f2582c0", tags=u"tag1, mytag",title="titre de l\'annotation",text="texte selectionne lors de la creation de l\'annotation",color="#AAAAAA", creation_date="2010-09-06T12:33:53.417550", update_date="2010-09-06T12:33:53.420459")
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   174
        self.annotation.save()
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   175
        self.c = Client()
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   176
    def tearDown(self):
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   177
        annotlist=Annotation.objects.all()
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   178
        for annot in annotlist:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   179
            annot.delete()
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   180
            
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   181
    def test_update_annotation(self):
16
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 10
diff changeset
   182
        content = '<iri><text-annotation><id></id><uri></uri><tags><tag>tag1</tag><tag>tag2new</tag><tag>tag3</tag></tags><content><color>#DDDDDD</color><description><![CDATA[texte de description update]]></description><title></title><text><![CDATA[texte selectionne a nouveau lors de la creation de l\'annotation]]></text></content><meta><contributor>oaubert</contributor><contributor-id>80cd0532-1dda-4130-b351-6a181130a7c9</contributor-id><created></created><creator></creator><creator-id></creator-id><modified>2010-11-06 12:33:53.420459</modified></meta></text-annotation></iri>'
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 10
diff changeset
   183
        response = self.c.post('/api/'+ VERSION_STR +'/text/update/', {'content':content,'id':'d2c1d1fa-629d-4520-a3d2-955b4f2582c0'})        
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   184
        doc = lxml.etree.fromstring(response.content)
16
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 10
diff changeset
   185
        #self.assertEqual(lxml.etree.tostring(doc), " ")
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 10
diff changeset
   186
        self.assertEqual(doc.xpath("/iri/text-annotation/id/text()")[0],"d2c1d1fa-629d-4520-a3d2-955b4f2582c0")
17
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   187
        self.assertEqual(doc.xpath("/iri/text-annotation/tags/tag/text()")[1], "tag2new")
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   188
        self.assertEqual(doc.xpath("/iri/text-annotation/content/color/text()")[0],"#DDDDDD")
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   189
        
10
000f3ca19eaa Added 404 templates + some views corrections
wakimd
parents: 9
diff changeset
   190
    def test_error_update(self):
16
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 10
diff changeset
   191
        content = '<iri><text-annotation><id>d2c1d1fa-629d-4520-a3d2-955b4f2582c0</id><uri>http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168</uri><tags><tag>tag1</tag><tag>tag2</tag></tags><content><color>#AAAAAA</color><description><![CDATA[texte de description]]></description><title><![CDATA[titre de l\'annotation]]></title><text><![CDATA[texte selectionne lors de la creation de l\'annotation]]></text></content><meta><contributor>oaubert</contributor><contributor-id>79cd0532-1dda-4130-b351-6a181130a7c9</contributor-id><created>2010-09-06 12:33:53.417550</created><creator>oaubert</creator><creator-id>79cd0532-1dda-4130-b351-6a181130a7c9</creator-id><modified>2010-09-06 12:33:53.420459</modified></meta></text-annotation></iri>'
10
000f3ca19eaa Added 404 templates + some views corrections
wakimd
parents: 9
diff changeset
   192
        response = self.c.post('/api/'+ VERSION_STR +'/text/update/', {'content':content,'id':'1'})
000f3ca19eaa Added 404 templates + some views corrections
wakimd
parents: 9
diff changeset
   193
        self.assertEqual(response.status_code,404)
000f3ca19eaa Added 404 templates + some views corrections
wakimd
parents: 9
diff changeset
   194
               
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   195
16
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 10
diff changeset
   196
class OnServerGlobalTest(unittest.TestCase):
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 10
diff changeset
   197
    def setUp(self):
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 10
diff changeset
   198
        self.content = urllib.urlencode({'content':'<iri><text-annotation><id>mypersonnalid</id><uri>http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168</uri><tags><tag>tag1</tag><tag>tag2</tag></tags><content><color>#AAAAAA</color><description><![CDATA[texte de description]]></description><title><![CDATA[titre de l\'annotation]]></title><text><![CDATA[texte selectionne lors de la creation de l\'annotation]]></text></content><meta><contributor>oaubert</contributor><contributor-id>79cd0532-1dda-4130-b351-6a181130a7c9</contributor-id><created>2010-09-06 12:33:53.417550</created><creator>oaubert</creator><creator-id>79cd0532-1dda-4130-b351-6a181130a7c9</creator-id><modified>2010-09-06 12:33:53.420459</modified></meta></text-annotation></iri>'})
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 10
diff changeset
   199
        self.content2 = urllib.urlencode({'content':'<iri><text-annotation><id>mypersonnalid2</id><uri>http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168</uri><tags><tag>tag1</tag><tag>tag2</tag></tags><content><color>#BBBBBB</color><description><![CDATA[texte de description2]]></description><title><![CDATA[titre de l\'annotation2]]></title><text><![CDATA[texte selectionne lors de la creation de l\'annotation2]]></text></content><meta><contributor>wakimd</contributor><contributor-id>79cd0532-1dda-4130-b351-6a181130a7c9</contributor-id><created>2010-09-06 12:33:53.417550</created><creator>oaubert</creator><creator-id>79cd0532-1dda-4130-b351-6a181130a7c9</creator-id><modified>2010-09-06 12:33:53.420459</modified></meta></text-annotation></iri>'})
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 10
diff changeset
   200
        self.id = urllib.urlencode({"id":"mypersonnalid"})
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 10
diff changeset
   201
        self.id2 = urllib.urlencode({"id":"mypersonnalid2"})
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 10
diff changeset
   202
        self.uri = urllib.urlencode({"uri":"http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168"})
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 10
diff changeset
   203
        self.filt1 = urllib.urlencode({"uri":"http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168", "creator":"","limit":"","filter":""})
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 10
diff changeset
   204
        self.filt2 = urllib.urlencode({"uri":"http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168","creator":"wakimd","limit":"","filter":""})
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 10
diff changeset
   205
        self.up = urllib.urlencode({'content':'<iri><text-annotation><id></id><uri></uri><tags><tag>tag1</tag><tag>tag2new</tag><tag>tag3</tag></tags><content><color>#DDDDDD</color><description><![CDATA[texte de description update]]></description><title></title><text><![CDATA[texte selectionne a nouveau lors de la creation de l\'annotation]]></text></content><meta><contributor>oaubert</contributor><contributor-id>80cd0532-1dda-4130-b351-6a181130a7c9</contributor-id><created></created><creator></creator><creator-id></creator-id><modified>2010-11-06 12:33:53.420459</modified></meta></text-annotation></iri>','id':'mypersonnalid'})
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 10
diff changeset
   206
    
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 10
diff changeset
   207
    def test_everything(self):
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 10
diff changeset
   208
        creation = urllib.urlopen("http://127.0.0.1:8000/api/"+VERSION_STR+"/text/create/", self.content)
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 10
diff changeset
   209
        creation2 = urllib.urlopen("http://127.0.0.1:8000/api/"+VERSION_STR+"/text/create/", self.content2)
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 10
diff changeset
   210
        
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 10
diff changeset
   211
        get = urllib.urlopen("http://127.0.0.1:8000/api/"+VERSION_STR+"/text/get/?%s" % self.id)
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 10
diff changeset
   212
        
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 10
diff changeset
   213
        update = urllib.urlopen("http://127.0.0.1:8000/api/"+VERSION_STR+"/text/update/", self.up)
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 10
diff changeset
   214
        
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 10
diff changeset
   215
        filt1 = urllib.urlopen("http://127.0.0.1:8000/api/"+VERSION_STR+"/text/filter/?%s", self.uri)
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 10
diff changeset
   216
        filt2 = urllib.urlopen("http://127.0.0.1:8000/api/"+VERSION_STR+"/text/filter/?uri=http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168?creator=wakimd")
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 10
diff changeset
   217
        tmp = open('debug.html','r+')
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 10
diff changeset
   218
        tmp.write(filt2.read())
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 10
diff changeset
   219
        
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 10
diff changeset
   220
        delete = urllib.urlopen("http://127.0.0.1:8000/api/"+VERSION_STR+"/text/delete/", self.id)
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 10
diff changeset
   221
        delete = urllib.urlopen("http://127.0.0.1:8000/api/"+VERSION_STR+"/text/delete/", self.id2)
19
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   222
16
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 10
diff changeset
   223
        
19
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   224
class OauthTestDelete(unittest.TestCase):
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   225
    def setUp(self):
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   226
        #create a user
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   227
        self.jane = User.objects.create_user('jane', 'jane@example.com', 'toto')
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   228
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   229
        resource = Resource(name='delete', url='/api/1.0/text/delete/')
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   230
        resource.save()
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   231
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   232
        self.CONSUMER_KEY = 'dpf43f3p2l4k3l03'
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   233
        self.CONSUMER_SECRET = 'kd94hf93k423kf44'
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   234
        self.consumer = Consumer(key=self.CONSUMER_KEY, secret=self.CONSUMER_SECRET, name='printer.example.com', user=self.jane)
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   235
        self.consumer.save()
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   236
        
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   237
        self.nonce = generate_nonce(8)
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   238
        
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   239
        #auth parameters
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   240
        self.parameters = {
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   241
            'oauth_consumer_key': self.CONSUMER_KEY,
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   242
            'oauth_signature_method': 'PLAINTEXT',
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   243
            'oauth_signature': '%s&' % self.CONSUMER_SECRET,
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   244
            'oauth_timestamp': str(int(time.time())),
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   245
            'oauth_nonce': self.nonce,
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   246
            'oauth_version': '1.0',
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   247
            'oauth_callback': 'http://printer.example.com/request_token_ready',
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   248
            'scope':'delete'
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   249
        }
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   250
        
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   251
        #test client
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   252
        self.c = Client()
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   253
        
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   254
        self.annotation = Annotation(external_id="d2c1d1fa-629d-4520-a3d2-955b4f2582c0",title="titre de l\'annotation",text="texte selectionne lors de la creation de l\'annotation",color="#AAAAAA", creation_date="2010-09-06T12:33:53.417550", update_date="2010-09-06T12:33:53.420459")
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   255
        self.annotation.save()
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   256
        
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   257
    def tearDown(self):
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   258
        Token.objects.all().delete()
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   259
        Resource.objects.all().delete()
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   260
        Consumer.objects.all().delete()
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   261
        Nonce.objects.all().delete()
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   262
        User.objects.all().delete()
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   263
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   264
        
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   265
    def test_auth_access_delete(self):
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   266
        ## REQUEST TOKEN
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   267
        
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   268
        response = self.c.get("/oauth/request_token/", self.parameters)
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   269
        #self.assertEqual(response.content,"  ")
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   270
        self.assertEqual(response.status_code,200)   
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   271
        token = list(Token.objects.all())[-1]
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   272
        self.assertTrue(token.key in response.content)
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   273
        self.assertTrue(token.secret in response.content)
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   274
        self.assertEqual(token.callback, u'http://printer.example.com/request_token_ready'),
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   275
        self.assertTrue(token.callback_confirmed)
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   276
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   277
#        token.callback = OUT_OF_BAND
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   278
#        token.save()
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   279
#        
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   280
        ## USER AUTHORIZATION
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   281
        
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   282
        parameters = {
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   283
            'oauth_token': token.key,
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   284
        }
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   285
        
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   286
        response = self.c.get("/oauth/authorize/", parameters)
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   287
        self.assertEqual(response.status_code,302)
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   288
        self.assertTrue(token.key in response['Location'])
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   289
        
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   290
        self.c.login(username='jane', password='toto')
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   291
        
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   292
        response = self.c.get("/oauth/authorize/", parameters)
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   293
        self.assertEqual(response.status_code,200)
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   294
        self.assertEqual(response.content,'Fake authorize view for printer.example.com.')
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   295
    
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   296
#        parameters['authorize_access'] = 0
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   297
#        response = self.c.post("/oauth/authorize/", parameters)
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   298
#        self.assertEqual(response.content, "Fake callback view.")
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   299
        
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   300
        # fake authorization by the user
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   301
        parameters['authorize_access'] = 1
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   302
        response = self.c.post("/oauth/authorize/", parameters)
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   303
        self.assertEqual(response.status_code,302)
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   304
        token = list(Token.objects.all())[-1]
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   305
        self.assertTrue(token.key in response['Location'])
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   306
        self.assertTrue(token.is_approved)
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   307
        
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   308
        ## ACCESS TOKEN
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   309
        
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   310
        parameters = {
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   311
            'oauth_consumer_key': self.CONSUMER_KEY,
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   312
            'oauth_token': token.key,
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   313
            'oauth_signature_method': 'PLAINTEXT',
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   314
            'oauth_signature': '%s&%s' % (self.CONSUMER_SECRET, token.secret),
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   315
            'oauth_timestamp': str(int(time.time())),
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   316
            'oauth_nonce': self.nonce,
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   317
            'oauth_version': '1.0',
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   318
            'oauth_verifier': token.verifier,
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   319
        }
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   320
        response = self.c.get("/oauth/access_token/", parameters)
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   321
        
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   322
        access_token = list(Token.objects.filter(token_type=Token.ACCESS))[-1]
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   323
        self.assertTrue(access_token.key in response.content)
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   324
        self.assertTrue(access_token.secret in response.content)
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   325
        self.assertEqual(access_token.user.username, u'jane')
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   326
        
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   327
        ## ACCESSING PROTECTED VIEW
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   328
        
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   329
        parameters = {
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   330
            'oauth_consumer_key': self.CONSUMER_KEY,
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   331
            'oauth_token': access_token.key,
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   332
            'oauth_signature_method': 'HMAC-SHA1',
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   333
            'oauth_timestamp': str(int(time.time())),
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   334
            'oauth_nonce': self.nonce,
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   335
            'oauth_version': '1.0',
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   336
        }
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   337
        
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   338
        oauth_request = OAuthRequest.from_token_and_callback(access_token, http_url='/api/1.0/text/delete/', parameters=parameters)
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   339
        signature_method = OAuthSignatureMethod_HMAC_SHA1()
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   340
        signature = signature_method.build_signature(oauth_request, self.consumer, access_token)
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   341
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   342
        parameters['oauth_signature'] = signature
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   343
        #self.assertEqual(signature, "  ")
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   344
        parameters['id'] = 'd2c1d1fa-629d-4520-a3d2-955b4f2582c0'
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   345
        response = self.c.post("/api/1.0/text/delete/", parameters)
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   346
        self.assertEqual(response.content, "  ")
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   347
        self.assertEqual(response.status_code,200)
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   348
        
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   349
        self.c.logout()
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   350
        access_token.delete()
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   351
#/api/1.0/text/delete/
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   352
#/api/1.0/text/update/
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
   353
#/api/1.0/text/create/