web/ldt/text/tests.py
changeset 21 1a061f244254
parent 19 7cf81d58a968
equal deleted inserted replaced
20:20c41a7e2173 21:1a061f244254
    16 from django.conf import settings
    16 from django.conf import settings
    17 from django.test.client import Client
    17 from django.test.client import Client
    18 from ldt.text import VERSION_STR
    18 from ldt.text import VERSION_STR
    19 from django.db import transaction
    19 from django.db import transaction
    20 from django.contrib.auth.models import User
    20 from django.contrib.auth.models import User
    21 from oauth_provider.models import Resource, Consumer
       
    22 import time
    21 import time
    23 from oauth_provider.models import Token
    22 import lucene
    24 from oauth.oauth import OAuthRequest, OAuthSignatureMethod_HMAC_SHA1
    23 from ldt.text import STORE, ANALYZER
    25 from django.contrib.auth.models import User
    24 from ldt.text.utils import *
    26 from oauth_provider.models import Resource, Consumer, Token, Nonce
       
    27 import time
       
    28 from oauth_provider.consts import OUT_OF_BAND
       
    29 from oauth.oauth import OAuthRequest, OAuthSignatureMethod_PLAINTEXT, generate_nonce
       
    30 
    25 
    31 
    26 
    32 # This test creates an annotation and checks that:
    27 # This test creates an annotation and checks that:
    33 # 1. the annotation was created in the database (by trying to access it through a 'get')
    28 # 1. the annotation was created in the database (by trying to access it through a 'get')
    34 # 2. the returned xml contains correct data
    29 # 2. the returned xml contains correct data
   127             
   122             
   128     def test_filter_annotation_filter(self):
   123     def test_filter_annotation_filter(self):
   129         uri = "http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168"
   124         uri = "http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168"
   130         filter = 'lors'
   125         filter = 'lors'
   131         limit = None
   126         limit = None
   132         response = self.c.get('/api/'+ VERSION_STR +'/text/filter/', {'uri':uri,'filter':'lors'})
   127         response = self.c.get('/api/'+ VERSION_STR +'/text/filter/', {'uri':uri,'filter':filter})
   133         doc = lxml.etree.fromstring(response.content)
   128         doc = lxml.etree.fromstring(response.content)
   134         for elem in doc.xpath("/iri/text-annotation/content/text/text()"):
   129         for elem in doc.xpath("/iri/text-annotation/content/text/text()"):
   135             self.assertTrue('lors' in elem)  
   130             self.assertTrue('lors' in elem)  
   136         #for elem in doc.xpath("/iri/text-annotation/meta/creator/text()"):
   131         #for elem in doc.xpath("/iri/text-annotation/meta/creator/text()"):
   137         #    self.assertEqual(elem,user)          
   132         #    self.assertEqual(elem,user)          
   142 # 2. the returned xml contains no data
   137 # 2. the returned xml contains no data
   143 class DeleteTest(unittest.TestCase):
   138 class DeleteTest(unittest.TestCase):
   144     def setUp(self):
   139     def setUp(self):
   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")
   140         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")
   146         self.annotation.save()
   141         self.annotation.save()
   147         self.c = Client()    
   142         self.c = Client()
   148     def tearDown(self):
   143     def tearDown(self):
   149         annotlist=Annotation.objects.all()
   144         annotlist=Annotation.objects.all()
   150         for annot in annotlist:
   145         for annot in annotlist:
   151             annot.delete()
   146             annot.delete()
       
   147         
   152     
   148     
   153     def test_delete_annotation(self):
   149     def test_delete_annotation(self):
   154         id = urllib.urlencode({'id':'d2c1d1fa-629d-4520-a3d2-955b4f2582c0'})
   150         id = urllib.urlencode({'id':'d2c1d1fa-629d-4520-a3d2-955b4f2582c0'})
   155         f = urllib.urlopen("http://127.0.0.1:8000/api/1.0/text/delete/", id)
   151         f = urllib.urlopen("http://127.0.0.1:8000/api/1.0/text/delete/", id)
   156         response = self.c.post('/api/'+ VERSION_STR +'/text/delete/', {'id':'d2c1d1fa-629d-4520-a3d2-955b4f2582c0'})
   152         response = self.c.post('/api/'+ VERSION_STR +'/text/delete/', {'id':'d2c1d1fa-629d-4520-a3d2-955b4f2582c0'})
   158         doc = lxml.etree.fromstring(response.content)
   154         doc = lxml.etree.fromstring(response.content)
   159         self.assertEqual(doc.xpath("/iri/text-annotation/id/text()"),[])
   155         self.assertEqual(doc.xpath("/iri/text-annotation/id/text()"),[])
   160         self.assertEqual(doc.xpath("/iri/text-annotation/tags/tag/text()"), [])
   156         self.assertEqual(doc.xpath("/iri/text-annotation/tags/tag/text()"), [])
   161         self.assertEqual(doc.xpath("/iri/text-annotation/content/color/text()"),[])
   157         self.assertEqual(doc.xpath("/iri/text-annotation/content/color/text()"),[])
   162         self.assertEqual(doc.xpath("/iri/text-annotation/meta/creator/text()"),[])
   158         self.assertEqual(doc.xpath("/iri/text-annotation/meta/creator/text()"),[])
   163         self.assertEqual(response2.status_code, 404)   
   159         self.assertEqual(response2.status_code, 404)
       
   160 
   164 
   161 
   165     def test_error_delete(self):
   162     def test_error_delete(self):
   166         response = self.c.post('/api/'+ VERSION_STR +'/text/ldt/delete/', {'id':'1'})
   163         response = self.c.post('/api/'+ VERSION_STR +'/text/ldt/delete/', {'id':'1'})
   167         self.assertEqual(response.status_code,404)
   164         self.assertEqual(response.status_code,404)
   168         
   165         
   201         self.id2 = urllib.urlencode({"id":"mypersonnalid2"})
   198         self.id2 = urllib.urlencode({"id":"mypersonnalid2"})
   202         self.uri = urllib.urlencode({"uri":"http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168"})
   199         self.uri = urllib.urlencode({"uri":"http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168"})
   203         self.filt1 = urllib.urlencode({"uri":"http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168", "creator":"","limit":"","filter":""})
   200         self.filt1 = urllib.urlencode({"uri":"http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168", "creator":"","limit":"","filter":""})
   204         self.filt2 = urllib.urlencode({"uri":"http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168","creator":"wakimd","limit":"","filter":""})
   201         self.filt2 = urllib.urlencode({"uri":"http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168","creator":"wakimd","limit":"","filter":""})
   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'})
   202         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'})
       
   203         self.LS = LdtSearch()
   206     
   204     
   207     def test_everything(self):
   205     def test_everything(self):
   208         creation = urllib.urlopen("http://127.0.0.1:8000/api/"+VERSION_STR+"/text/create/", self.content)
   206         creation = urllib.urlopen("http://127.0.0.1:8000/api/"+VERSION_STR+"/text/create/", self.content)
   209         creation2 = urllib.urlopen("http://127.0.0.1:8000/api/"+VERSION_STR+"/text/create/", self.content2)
   207         creation2 = urllib.urlopen("http://127.0.0.1:8000/api/"+VERSION_STR+"/text/create/", self.content2)
       
   208 
       
   209         res1 = self.LS.query("title","titre de l'annotation")
       
   210         self.assertEqual(len(res1),1)
       
   211         res2 = self.LS.query("title","titre de l'annotation2")
       
   212         self.assertEqual(len(res2),1)
       
   213 
   210         
   214         
   211         get = urllib.urlopen("http://127.0.0.1:8000/api/"+VERSION_STR+"/text/get/?%s" % self.id)
   215         get = urllib.urlopen("http://127.0.0.1:8000/api/"+VERSION_STR+"/text/get/?%s" % self.id)
   212         
   216         
   213         update = urllib.urlopen("http://127.0.0.1:8000/api/"+VERSION_STR+"/text/update/", self.up)
   217         update = urllib.urlopen("http://127.0.0.1:8000/api/"+VERSION_STR+"/text/update/", self.up)
   214         
   218         
   215         filt1 = urllib.urlopen("http://127.0.0.1:8000/api/"+VERSION_STR+"/text/filter/?%s", self.uri)
   219         res3 = self.LS.query("abstract","texte de description update")
       
   220         self.assertEqual(len(res3),1)
       
   221         
       
   222         filt1 = urllib.urlopen("http://127.0.0.1:8000/api/"+VERSION_STR+"/text/filter/?%s" % self.uri)
   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")
   223         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")
   217         tmp = open('debug.html','r+')
   224 
   218         tmp.write(filt2.read())
       
   219         
       
   220         delete = urllib.urlopen("http://127.0.0.1:8000/api/"+VERSION_STR+"/text/delete/", self.id)
   225         delete = urllib.urlopen("http://127.0.0.1:8000/api/"+VERSION_STR+"/text/delete/", self.id)
   221         delete = urllib.urlopen("http://127.0.0.1:8000/api/"+VERSION_STR+"/text/delete/", self.id2)
   226         delete = urllib.urlopen("http://127.0.0.1:8000/api/"+VERSION_STR+"/text/delete/", self.id2)
   222 
   227 
   223         
   228         res4 = self.LS.query("title","titre de l'annotation")
   224 class OauthTestDelete(unittest.TestCase):
   229         self.assertEqual(len(res4),0)
   225     def setUp(self):
   230         res5 = self.LS.query("title","titre de l'annotation2")
   226         #create a user
   231         self.assertEqual(len(res5),0)
   227         self.jane = User.objects.create_user('jane', 'jane@example.com', 'toto')
   232 
   228 
   233         
   229         resource = Resource(name='delete', url='/api/1.0/text/delete/')
   234         
   230         resource.save()
       
   231 
       
   232         self.CONSUMER_KEY = 'dpf43f3p2l4k3l03'
       
   233         self.CONSUMER_SECRET = 'kd94hf93k423kf44'
       
   234         self.consumer = Consumer(key=self.CONSUMER_KEY, secret=self.CONSUMER_SECRET, name='printer.example.com', user=self.jane)
       
   235         self.consumer.save()
       
   236         
       
   237         self.nonce = generate_nonce(8)
       
   238         
       
   239         #auth parameters
       
   240         self.parameters = {
       
   241             'oauth_consumer_key': self.CONSUMER_KEY,
       
   242             'oauth_signature_method': 'PLAINTEXT',
       
   243             'oauth_signature': '%s&' % self.CONSUMER_SECRET,
       
   244             'oauth_timestamp': str(int(time.time())),
       
   245             'oauth_nonce': self.nonce,
       
   246             'oauth_version': '1.0',
       
   247             'oauth_callback': 'http://printer.example.com/request_token_ready',
       
   248             'scope':'delete'
       
   249         }
       
   250         
       
   251         #test client
       
   252         self.c = Client()
       
   253         
       
   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")
       
   255         self.annotation.save()
       
   256         
       
   257     def tearDown(self):
       
   258         Token.objects.all().delete()
       
   259         Resource.objects.all().delete()
       
   260         Consumer.objects.all().delete()
       
   261         Nonce.objects.all().delete()
       
   262         User.objects.all().delete()
       
   263 
       
   264         
       
   265     def test_auth_access_delete(self):
       
   266         ## REQUEST TOKEN
       
   267         
       
   268         response = self.c.get("/oauth/request_token/", self.parameters)
       
   269         #self.assertEqual(response.content,"  ")
       
   270         self.assertEqual(response.status_code,200)   
       
   271         token = list(Token.objects.all())[-1]
       
   272         self.assertTrue(token.key in response.content)
       
   273         self.assertTrue(token.secret in response.content)
       
   274         self.assertEqual(token.callback, u'http://printer.example.com/request_token_ready'),
       
   275         self.assertTrue(token.callback_confirmed)
       
   276 
       
   277 #        token.callback = OUT_OF_BAND
       
   278 #        token.save()
       
   279 #        
       
   280         ## USER AUTHORIZATION
       
   281         
       
   282         parameters = {
       
   283             'oauth_token': token.key,
       
   284         }
       
   285         
       
   286         response = self.c.get("/oauth/authorize/", parameters)
       
   287         self.assertEqual(response.status_code,302)
       
   288         self.assertTrue(token.key in response['Location'])
       
   289         
       
   290         self.c.login(username='jane', password='toto')
       
   291         
       
   292         response = self.c.get("/oauth/authorize/", parameters)
       
   293         self.assertEqual(response.status_code,200)
       
   294         self.assertEqual(response.content,'Fake authorize view for printer.example.com.')
       
   295     
       
   296 #        parameters['authorize_access'] = 0
       
   297 #        response = self.c.post("/oauth/authorize/", parameters)
       
   298 #        self.assertEqual(response.content, "Fake callback view.")
       
   299         
       
   300         # fake authorization by the user
       
   301         parameters['authorize_access'] = 1
       
   302         response = self.c.post("/oauth/authorize/", parameters)
       
   303         self.assertEqual(response.status_code,302)
       
   304         token = list(Token.objects.all())[-1]
       
   305         self.assertTrue(token.key in response['Location'])
       
   306         self.assertTrue(token.is_approved)
       
   307         
       
   308         ## ACCESS TOKEN
       
   309         
       
   310         parameters = {
       
   311             'oauth_consumer_key': self.CONSUMER_KEY,
       
   312             'oauth_token': token.key,
       
   313             'oauth_signature_method': 'PLAINTEXT',
       
   314             'oauth_signature': '%s&%s' % (self.CONSUMER_SECRET, token.secret),
       
   315             'oauth_timestamp': str(int(time.time())),
       
   316             'oauth_nonce': self.nonce,
       
   317             'oauth_version': '1.0',
       
   318             'oauth_verifier': token.verifier,
       
   319         }
       
   320         response = self.c.get("/oauth/access_token/", parameters)
       
   321         
       
   322         access_token = list(Token.objects.filter(token_type=Token.ACCESS))[-1]
       
   323         self.assertTrue(access_token.key in response.content)
       
   324         self.assertTrue(access_token.secret in response.content)
       
   325         self.assertEqual(access_token.user.username, u'jane')
       
   326         
       
   327         ## ACCESSING PROTECTED VIEW
       
   328         
       
   329         parameters = {
       
   330             'oauth_consumer_key': self.CONSUMER_KEY,
       
   331             'oauth_token': access_token.key,
       
   332             'oauth_signature_method': 'HMAC-SHA1',
       
   333             'oauth_timestamp': str(int(time.time())),
       
   334             'oauth_nonce': self.nonce,
       
   335             'oauth_version': '1.0',
       
   336         }
       
   337         
       
   338         oauth_request = OAuthRequest.from_token_and_callback(access_token, http_url='/api/1.0/text/delete/', parameters=parameters)
       
   339         signature_method = OAuthSignatureMethod_HMAC_SHA1()
       
   340         signature = signature_method.build_signature(oauth_request, self.consumer, access_token)
       
   341 
       
   342         parameters['oauth_signature'] = signature
       
   343         #self.assertEqual(signature, "  ")
       
   344         parameters['id'] = 'd2c1d1fa-629d-4520-a3d2-955b4f2582c0'
       
   345         response = self.c.post("/api/1.0/text/delete/", parameters)
       
   346         self.assertEqual(response.content, "  ")
       
   347         self.assertEqual(response.status_code,200)
       
   348         
       
   349         self.c.logout()
       
   350         access_token.delete()
       
   351 #/api/1.0/text/delete/
       
   352 #/api/1.0/text/update/
       
   353 #/api/1.0/text/create/