src/ldt/ldt/text/tests/server_tests.py
changeset 63 93325a5d61f0
parent 28 5cba2808cde0
equal deleted inserted replaced
62:27253368f045 63:93325a5d61f0
     1 #encoding:UTF-8
     1 #encoding:UTF-8
     2 
     2 
     3 """ Run these tests with 'python manage.py test text'  """
     3 """ Run these tests with 'python manage.py test text'  """
     4 
     4 
     5 from django.conf import settings
     5 from ldt.test.testcases import OAuthWebTestCase, WebTestCase
     6 from django.contrib.auth.models import *
       
     7 from django.test import TestCase
       
     8 from ldt.core.models import Owner
       
     9 from ldt.test.testcases import OAuthWebTestCase, OAuthTestCase, WebTestCase 
       
    10 from ldt.text import VERSION_STR
     6 from ldt.text import VERSION_STR
    11 from ldt.text.models import *
     7 from ldt.text.models import Annotation
    12 from ldt.text.views import *
       
    13 from oauth_provider.models import Resource, Consumer, Token, Nonce
       
    14 import datetime
     8 import datetime
    15 import logging
       
    16 import lxml.etree
     9 import lxml.etree
    17 import time
       
    18 import unittest
       
    19 import urllib
       
    20 import uuid
       
    21 
    10 
    22 CONSUMER_KEY = 'dpf43f3p2l4k3l03'
    11 CONSUMER_KEY = 'dpf43f3p2l4k3l03'
    23 CONSUMER_SECRET = 'kd94hf93k423kf44'
    12 CONSUMER_SECRET = 'kd94hf93k423kf44'
    24 
    13 
    25 
    14 
    26 class OnServerGlobalTest(OAuthWebTestCase):
    15 class OnServerGlobalTest(OAuthWebTestCase):
    27     
    16     
    28     fixtures = ['oauth_data','base_data']
    17     fixtures = ['oauth_data', 'base_data']
    29     
    18     
    30     def setUp(self):
    19     def setUp(self):
    31         
    20         
    32         self.id = "mypersonnalid"
    21         self.id = "mypersonnalid"
    33         self.title = "titre de l\'annotation"
    22         self.title = "titre de l\'annotation"
    35         self.textupdate = "texte selectionne a nouveau lors de la creation de l\'annotation"
    24         self.textupdate = "texte selectionne a nouveau lors de la creation de l\'annotation"
    36         self.descupdate = "texte de description update"
    25         self.descupdate = "texte de description update"
    37         self.contributor = "oaubert"
    26         self.contributor = "oaubert"
    38         self.creator = "wakimd"
    27         self.creator = "wakimd"
    39         self.uri = "http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168"
    28         self.uri = "http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168"
    40         self.content1 = '<iri><text-annotation><id>'+self.id+'</id><uri>'+self.uri+'</uri><tags><tag>tag1</tag><tag>tag2</tag></tags><content><color>#AAAAAA</color><description><![CDATA[texte de description]]></description><title>'+self.title+'</title><text>'+self.text+'</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>'+self.creator+'</creator><creator-id>79cd0532-1dda-4130-b351-6a181130a7c9</creator-id><modified>2010-09-06 12:33:53.420459</modified></meta></text-annotation></iri>'        
    29         self.content1 = '<iri><text-annotation><id>' + self.id + '</id><uri>' + self.uri + '</uri><tags><tag>tag1</tag><tag>tag2</tag></tags><content><color>#AAAAAA</color><description><![CDATA[texte de description]]></description><title>' + self.title + '</title><text>' + self.text + '</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>' + self.creator + '</creator><creator-id>79cd0532-1dda-4130-b351-6a181130a7c9</creator-id><modified>2010-09-06 12:33:53.420459</modified></meta></text-annotation></iri>'        
    41         self.contentupdate = '<iri><text-annotation><id></id><uri></uri><tags><tag>tag1</tag><tag>tag2new</tag><tag>tag3</tag></tags><content><color>#DDDDDD</color><description>'+self.descupdate+'</description><title></title><text>'+self.textupdate+'</text></content><meta><contributor>'+self.contributor+'</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>'
    30         self.contentupdate = '<iri><text-annotation><id></id><uri></uri><tags><tag>tag1</tag><tag>tag2new</tag><tag>tag3</tag></tags><content><color>#DDDDDD</color><description>' + self.descupdate + '</description><title></title><text>' + self.textupdate + '</text></content><meta><contributor>' + self.contributor + '</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>'
    42         
    31         
    43         self.set_login_url("/auth_accounts/login/")
    32         self.set_login_url("/auth_accounts/login/")
    44         self.set_consumer(CONSUMER_KEY, CONSUMER_SECRET)
    33         self.set_consumer(CONSUMER_KEY, CONSUMER_SECRET)
    45 
    34 
    46 
    35 
    47     def test_everything(self):
    36     def test_everything(self):
    48 
    37 
    49         self.assertTrue(self.client.login(username='jane', password='toto'))
    38         self.assertTrue(self.client.login(username='jane', password='toto'))
    50         
    39         
    51         #test POST
    40         #test POST
    52         creation = self.client.post("/api/"+VERSION_STR+"/text/create/", data={'content': self.content1})            
    41         creation = self.client.post("/api/" + VERSION_STR + "/text/create/", data={'content': self.content1})            
    53         self.assertEqual(creation.status_code,200)
    42         self.assertEqual(creation.status_code, 200)
    54         annot = Annotation.objects.get(external_id=self.id)
    43         annot = Annotation.objects.get(external_id=self.id)
    55         self.assertEqual(str(annot.creation_date)[:11], str(datetime.datetime.now())[:11])
    44         self.assertEqual(str(annot.creation_date)[:11], str(datetime.datetime.now())[:11])
    56         self.assertEqual(annot.text,self.text)
    45         self.assertEqual(annot.text, self.text)
    57         self.assertEqual(len(annot.tags.split(",")),2)
    46         self.assertEqual(len(annot.tags.split(",")), 2)
    58         
    47         
    59         #test GET
    48         #test GET
    60         get = self.client.get("/api/"+VERSION_STR+"/text/get/"+self.id+"")
    49         get = self.client.get("/api/" + VERSION_STR + "/text/get/" + self.id + "")
    61         self.assertEqual(get.status_code,200)
    50         self.assertEqual(get.status_code, 200)
    62         annot = lxml.etree.fromstring(get.content)
    51         annot = lxml.etree.fromstring(get.content)
    63         self.assertTrue('tag1','tag2' in annot.xpath("/iri/text-annotation/tags/tag/text()"))
    52         self.assertTrue('tag1', 'tag2' in annot.xpath("/iri/text-annotation/tags/tag/text()"))
    64         self.assertEqual(annot.xpath("/iri/text-annotation/content/title/text()")[0],self.title)
    53         self.assertEqual(annot.xpath("/iri/text-annotation/content/title/text()")[0], self.title)
    65         
    54         
    66         #test OPTIONS
    55         #test OPTIONS
    67         options = self.client.options("/api/"+VERSION_STR+"/text/get/"+self.id+"")
    56         options = self.client.options("/api/" + VERSION_STR + "/text/get/" + self.id + "")
    68         self.assertEqual(options.status_code,200)
    57         self.assertEqual(options.status_code, 200)
    69         self.assertEqual(options.content, get.content)
    58         self.assertEqual(options.content, get.content)
    70        
    59        
    71         #test HEAD
    60         #test HEAD
    72         head = self.client.head("/api/"+VERSION_STR+"/text/get/"+self.id+"")
    61         head = self.client.head("/api/" + VERSION_STR + "/text/get/" + self.id + "")
    73         self.assertEqual(head.status_code,200)
    62         self.assertEqual(head.status_code, 200)
    74         self.assertEqual(head['content-type'],'text/xml;charset=utf-8')
    63         self.assertEqual(head['content-type'], 'text/xml;charset=utf-8')
    75         self.assertEqual(head['content-language'],'fr-fr')
    64         self.assertEqual(head['content-language'], 'fr-fr')
    76         self.assertEqual(head.content,'')
    65         self.assertEqual(head.content, '')
    77                
    66                
    78         #test LOGOUT
    67         #test LOGOUT
    79         self.client.logout()
    68         self.client.logout()
    80         
    69         
    81         creation = self.client.post("/api/"+VERSION_STR+"/text/create/", data={'content': self.content1})            
    70         creation = self.client.post("/api/" + VERSION_STR + "/text/create/", data={'content': self.content1})            
    82         self.assertEqual(creation.status_code,401)
    71         self.assertEqual(creation.status_code, 401)
    83         
    72         
    84         self.assertTrue(self.client.login(username='jane', password='toto'))
    73         self.assertTrue(self.client.login(username='jane', password='toto'))
    85 
    74 
    86         # filter
    75         # filter
    87         filt1 = self.client.get("/api/"+VERSION_STR+"/text/filter/", data={"uri":self.uri})
    76         filt1 = self.client.get("/api/" + VERSION_STR + "/text/filter/", data={"uri":self.uri})
    88         self.assertEqual(filt1.status_code,200)
    77         self.assertEqual(filt1.status_code, 200)
    89         doc = lxml.etree.fromstring(filt1.content)
    78         doc = lxml.etree.fromstring(filt1.content)
    90         self.assertEqual(len(doc.xpath("/iri/text-annotation")),2)
    79         self.assertEqual(len(doc.xpath("/iri/text-annotation")), 2)
    91         for elem in doc.xpath("/iri/text-annotation/uri/text()"):
    80         for elem in doc.xpath("/iri/text-annotation/uri/text()"):
    92             self.assertEqual(elem,self.uri)
    81             self.assertEqual(elem, self.uri)
    93 
    82 
    94         filt2 = self.client.get("/api/"+VERSION_STR+"/text/filter/", data={"creator":self.contributor})
    83         filt2 = self.client.get("/api/" + VERSION_STR + "/text/filter/", data={"creator":self.contributor})
    95         self.assertEqual(filt2.status_code,200)
    84         self.assertEqual(filt2.status_code, 200)
    96         self.assertEqual(filt2.content, '<iri/>\n')
    85         self.assertEqual(filt2.content, '<iri/>\n')
    97 
    86 
    98         #test PUT
    87         #test PUT
    99         update = self.client.put("/api/"+VERSION_STR+"/text/update/"+self.id+"", data={'content':self.contentupdate})
    88         update = self.client.put("/api/" + VERSION_STR + "/text/update/" + self.id + "", data={'content':self.contentupdate})
   100         self.assertEqual(update.status_code,200)
    89         self.assertEqual(update.status_code, 200)
   101         annot = Annotation.objects.get(external_id=self.id)
    90         annot = Annotation.objects.get(external_id=self.id)
   102         self.assertEqual(str(annot.creation_date)[:11],str(datetime.datetime.now())[:11])
    91         self.assertEqual(str(annot.creation_date)[:11], str(datetime.datetime.now())[:11])
   103         self.assertEqual(annot.external_id,self.id)
    92         self.assertEqual(annot.external_id, self.id)
   104         self.assertTrue('tag1','tag2' not in annot.tags)
    93         self.assertTrue('tag1', 'tag2' not in annot.tags)
   105         self.assertTrue('mytag','tag2new' in annot.tags)
    94         self.assertTrue('mytag', 'tag2new' in annot.tags)
   106         self.assertEqual(annot.description,self.descupdate)
    95         self.assertEqual(annot.description, self.descupdate)
   107         self.assertEqual(annot.text,self.textupdate)
    96         self.assertEqual(annot.text, self.textupdate)
   108         self.assertEqual(annot.contributor,self.contributor)
    97         self.assertEqual(annot.contributor, self.contributor)
   109         
    98         
   110         #test DELETE
    99         #test DELETE
   111         delete = self.client.delete("/api/"+VERSION_STR+"/text/delete/"+self.id+"")
   100         delete = self.client.delete("/api/" + VERSION_STR + "/text/delete/" + self.id + "")
   112         self.assertEqual(delete.content, "")
   101         self.assertEqual(delete.content, "")
   113         self.assertEqual(delete.status_code,200)   
   102         self.assertEqual(delete.status_code, 200)   
   114         self.assertRaises(Annotation.DoesNotExist, Annotation.objects.get, external_id=self.id)  
   103         self.assertRaises(Annotation.DoesNotExist, Annotation.objects.get, external_id=self.id)  
   115 
   104 
   116         self.client.logout()
   105         self.client.logout()
   117 
   106 
   118 
   107 
   119     def test_errors(self):
   108     def test_errors(self):
   120         
   109         
   121         self.assertTrue(self.client.login(username='jane', password='toto'))
   110         self.assertTrue(self.client.login(username='jane', password='toto'))
   122         
   111         
   123         creation = self.client.post("/api/"+VERSION_STR+"/text/create/", data={'content': self.content1})            
   112         creation = self.client.post("/api/" + VERSION_STR + "/text/create/", data={'content': self.content1})            
   124         self.assertEqual(creation.status_code,200)
   113         self.assertEqual(creation.status_code, 200)
   125         
   114         
   126         creation = self.client.post("/api/"+VERSION_STR+"/text/create/", data={'content': self.content1})            
   115         creation = self.client.post("/api/" + VERSION_STR + "/text/create/", data={'content': self.content1})            
   127         self.assertEqual(creation.status_code,409)
   116         self.assertEqual(creation.status_code, 409)
   128         
   117         
   129         get = self.client.get("/api/"+VERSION_STR+"/text/get/1")
   118         get = self.client.get("/api/" + VERSION_STR + "/text/get/1")
   130         self.assertEqual(get.status_code,404)
   119         self.assertEqual(get.status_code, 404)
   131         
   120         
   132         update = self.client.put('/api/'+ VERSION_STR +'/text/update/1', {'content':self.contentupdate})
   121         update = self.client.put('/api/' + VERSION_STR + '/text/update/1', {'content':self.contentupdate})
   133         self.assertEqual(update.status_code,404)
   122         self.assertEqual(update.status_code, 404)
   134         
   123         
   135         delete = self.client.delete("/api/"+VERSION_STR+"/text/delete/1")
   124         delete = self.client.delete("/api/" + VERSION_STR + "/text/delete/1")
   136         self.assertEqual(delete.status_code,404)
   125         self.assertEqual(delete.status_code, 404)
   137         
   126         
   138         delete = self.client.delete("/api/"+VERSION_STR+"/text/delete/"+self.id+"")
   127         delete = self.client.delete("/api/" + VERSION_STR + "/text/delete/" + self.id + "")
   139         self.assertEqual(delete.status_code,200)
   128         self.assertEqual(delete.status_code, 200)
   140         self.assertEqual(delete.content, "")
   129         self.assertEqual(delete.content, "")
   141         
   130         
   142         self.client.logout()
   131         self.client.logout()
   143 
   132 
   144 
   133 
   150         self.set_login_url("/auth_accounts/login/")
   139         self.set_login_url("/auth_accounts/login/")
   151     
   140     
   152     def test_get(self):
   141     def test_get(self):
   153         self.assertTrue(self.client.login(username='jane', password='toto'))
   142         self.assertTrue(self.client.login(username='jane', password='toto'))
   154         
   143         
   155         get = self.client.get("/api/"+VERSION_STR+"/text/get/z2c1d1fa-629d-4520-a3d2-955b4f2582c0")
   144         get = self.client.get("/api/" + VERSION_STR + "/text/get/z2c1d1fa-629d-4520-a3d2-955b4f2582c0")
   156         self.assertEqual(get.status_code,200)
   145         self.assertEqual(get.status_code, 200)
   157         annot = lxml.etree.fromstring(get.content)
   146         annot = lxml.etree.fromstring(get.content)
   158         self.assertTrue('tag1','tag2' in annot.xpath("/iri/text-annotation/tags/tag/text()"))
   147         self.assertTrue('tag1', 'tag2' in annot.xpath("/iri/text-annotation/tags/tag/text()"))
   159         
   148         
   160         
   149