src/cm/tests/test_api.py
author Simon Descarpentries <sid@sopinspace.com>
Tue, 06 May 2014 13:52:01 +0200
changeset 651 9bbc657f6837
parent 606 52f3e090eed9
permissions -rw-r--r--
Replace DISABLE_TRACKING and TRACKING_HTML by a TRACKING_ID variable in configuration files
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
287
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
     1
from django.test import TestCase
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
     2
from django.test.client import Client
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
     3
from django.core import management
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
     4
from datetime import datetime
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
     5
from cm.activity import *
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
     6
from cm.models import *
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
     7
from cm.security import *
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
     8
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
     9
from django.http import HttpRequest, HttpResponse
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    10
from django.utils import simplejson
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    11
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    12
from cm.api.handlers import *
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    13
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    14
#from piston.test import TestCase
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    15
from piston.models import Consumer
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    16
from piston.handler import BaseHandler
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    17
from piston.utils import rc
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    18
from piston.resource import Resource
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    19
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    20
class FalseRequest(object):
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    21
    def __init__(self, user):
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    22
        self.user = user
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    23
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    24
class APITest(TestCase):
606
52f3e090eed9 make django tests working again, from ymh, see http://www.iri.centrepompidou.fr/dev/hg/comt/rev/e103299bccc0
gibus
parents: 293
diff changeset
    25
    fixtures = ['initial_data','roles_generic', 'test_content', ]
287
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    26
    
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    27
    def test_text_get(self):
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    28
        """
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    29
        Anonymous api call
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    30
        """
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    31
        
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    32
        resource = Resource(AnonymousTextHandler)
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    33
        request = HttpRequest()
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    34
        setattr(request, 'user' , None)
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    35
        request.method = 'GET'
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    36
        
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    37
        # get public text
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    38
        response = resource(request, key='text_key_4', emitter_format='json')
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    39
        self.assertEquals(200, response.status_code) # 401: forbidden
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    40
        response_data = simplejson.loads(response.content)
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    41
        self.assertEquals(response_data.get('created'), '2009-02-13 04:01:12')
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    42
        
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    43
        # error: private text
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    44
        response = resource(request, key='text_key_3', emitter_format='json')
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    45
        self.assertEquals(401, response.status_code)
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    46
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    47
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    48
    def test_text_get_logged_in(self):
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    49
        """
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    50
        Logged in as manager api call
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    51
        """
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    52
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    53
        resource = Resource(AnonymousTextHandler)
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    54
        request = HttpRequest()
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    55
        user = User.objects.get(pk=1)
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    56
        setattr(request, 'user' , user)
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    57
        request.method = 'GET'
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    58
  
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    59
        response = resource(request, key='text_key_3', emitter_format='json')
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    60
        self.assertEquals(200, response.status_code)
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    61
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    62
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    63
    def test_text_create(self):
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    64
        request = FalseRequest(None) 
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    65
        nb_anon_texts = get_texts_with_perm(request, 'can_view_text').count()
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    66
        nb_texts = Text.objects.count()
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    67
        
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    68
        resource = Resource(TextListHandler)
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    69
        
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    70
        # create one private text 
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    71
        request = HttpRequest()
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    72
        user = User.objects.get(pk=1)
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    73
        setattr(request, 'user' , user)
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    74
        request.method = 'POST'
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    75
        setattr(request, 'POST' , {'content':'test content', 'format':"markdown", 'title': 'my title'})
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    76
        response = resource(request,)
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    77
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    78
        self.assertEquals(200, response.status_code)
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    79
        self.assertTrue('key' in simplejson.loads(response.content).keys())
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    80
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    81
        request = FalseRequest(None) 
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    82
        self.assertEqual(get_texts_with_perm(request, 'can_view_text').count(), nb_anon_texts) # NO more anon text
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    83
        
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    84
        # create one text with anon observer
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    85
        request = HttpRequest()
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    86
        user = User.objects.get(pk=1)
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    87
        setattr(request, 'user' , user)
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    88
        request.method = 'POST'
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    89
        setattr(request, 'POST' , {'content':'test content', 'format':"markdown", 'title': 'my title', 'anon_role' : 4})
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    90
        response = resource(request,)
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    91
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    92
        self.assertEquals(200, response.status_code)
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    93
        self.assertTrue('key' in simplejson.loads(response.content).keys())
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    94
        
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    95
        self.assertEquals(nb_texts + 2, Text.objects.count()) # 2 more texts should have been created
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    96
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    97
        request = FalseRequest(None) 
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    98
        self.assertEqual(get_texts_with_perm(request, 'can_view_text').count(), nb_anon_texts + 1) # one more anon accessible text available
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
    99
        
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   100
    def test_list_text_get(self):
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   101
        """
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   102
        List texts anon
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   103
        """
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   104
        resource = Resource(AnonymousTextListHandler)
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   105
        request = HttpRequest()
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   106
        setattr(request, 'user' , None)
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   107
        request.method = 'GET'
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   108
  
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   109
        response = resource(request, emitter_format='json')
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   110
        self.assertEquals(200, response.status_code)
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   111
        self.assertEquals(2, len(simplejson.loads(response.content)))
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   112
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   113
    def test_list_text_logged_in(self):
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   114
        """
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   115
        List texts manager
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   116
        """
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   117
        resource = Resource(AnonymousTextListHandler)
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   118
        request = HttpRequest()
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   119
        user = User.objects.get(pk=1)
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   120
        setattr(request, 'user' , user)
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   121
        request.method = 'GET'
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   122
  
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   123
        response = resource(request, emitter_format='json')
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   124
        self.assertEquals(200, response.status_code)
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   125
        self.assertEquals(5, len(simplejson.loads(response.content)))
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   126
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   127
    def test_delete_text_logged_in_works(self):
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   128
        """
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   129
        Delete text
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   130
        """
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   131
        nb_texts = Text.objects.count()
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   132
        
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   133
        resource = Resource(TextDeleteHandler)
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   134
        request = HttpRequest()
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   135
        user = User.objects.get(pk=1)
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   136
        setattr(request, 'user' , user)
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   137
        request.method = 'POST'
293
2c52e4453bf7 api update (incl. doc)
raph
parents: 287
diff changeset
   138
        setattr(request, 'POST' , {})
287
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   139
        setattr(request, 'flash' , {})
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   140
  
293
2c52e4453bf7 api update (incl. doc)
raph
parents: 287
diff changeset
   141
        response = resource(request, key='text_key_3', emitter_format='json')
287
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   142
        self.assertEquals(204, response.status_code)
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   143
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   144
        # one text deleted
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   145
        self.assertEquals(nb_texts - 1, Text.objects.count())
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   146
        
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   147
    def test_delete_text_logged_in_fail(self):
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   148
        """
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   149
        Delete text (and fail: insufficient rights)
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   150
        """
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   151
        nb_texts = Text.objects.count()
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   152
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   153
        resource = Resource(TextDeleteHandler)
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   154
        request = HttpRequest()
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   155
        user = User.objects.get(pk=3)
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   156
        setattr(request, 'user' , user)
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   157
        request.method = 'POST'
293
2c52e4453bf7 api update (incl. doc)
raph
parents: 287
diff changeset
   158
        setattr(request, 'POST' , {})
287
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   159
        setattr(request, 'flash' , {})
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   160
  
293
2c52e4453bf7 api update (incl. doc)
raph
parents: 287
diff changeset
   161
        response = resource(request, key='text_key_3', emitter_format='json')
287
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   162
        self.assertEquals(401, response.status_code)
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   163
        
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   164
        # no text deleted
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   165
        self.assertEquals(nb_texts, Text.objects.count())
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   166
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   167
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   168
    def test_pre_edit(self):
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   169
        """
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   170
        Pre edit text: should return number of comments to remove
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   171
        """
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   172
        resource = Resource(TextPreEditHandler)
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   173
        request = HttpRequest()
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   174
        user = User.objects.get(pk=1) 
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   175
        setattr(request, 'user' , user)
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   176
        request.method = 'POST'
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   177
        setattr(request, 'POST' , {'new_format' : 'markdown', 'new_content' : u'ggg'})
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   178
        setattr(request, 'flash' , {})
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   179
    
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   180
        response = resource(request, key='text_key_2', emitter_format='json')
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   181
        self.assertEquals(response.content, '{"nb_removed": 3}')
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   182
293
2c52e4453bf7 api update (incl. doc)
raph
parents: 287
diff changeset
   183
    def test_edit(self):
287
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   184
        """
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   185
        Edit text
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   186
        """
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   187
        resource = Resource(TextEditHandler)
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   188
        request = HttpRequest()
293
2c52e4453bf7 api update (incl. doc)
raph
parents: 287
diff changeset
   189
        session = {}
2c52e4453bf7 api update (incl. doc)
raph
parents: 287
diff changeset
   190
        setattr(request,'session',{})
2c52e4453bf7 api update (incl. doc)
raph
parents: 287
diff changeset
   191
        
287
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   192
        user = User.objects.get(pk=1) 
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   193
        setattr(request, 'user' , user)
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   194
        request.method = 'POST'
293
2c52e4453bf7 api update (incl. doc)
raph
parents: 287
diff changeset
   195
        setattr(request, 'POST' , {'format' : 'markdown', 'content' : u'ggg', 'keep_comments' : True, 'new_version' : True, 'title' : 'new title'})
2c52e4453bf7 api update (incl. doc)
raph
parents: 287
diff changeset
   196
        #setattr(request, 'flash' , {})
287
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   197
    
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   198
        response = resource(request, key='text_key_2', emitter_format='json')
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   199
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   200
        self.assertEquals(Text.objects.get(pk=2).last_text_version.content , u'ggg')
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   201
293
2c52e4453bf7 api update (incl. doc)
raph
parents: 287
diff changeset
   202
    def test_text_version(self):
287
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   203
        """
293
2c52e4453bf7 api update (incl. doc)
raph
parents: 287
diff changeset
   204
        Text version operation
287
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   205
        """
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   206
        from django.test.client import Client
fc5ed157ebfe add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff changeset
   207
        c = Client()
293
2c52e4453bf7 api update (incl. doc)
raph
parents: 287
diff changeset
   208
        
2c52e4453bf7 api update (incl. doc)
raph
parents: 287
diff changeset
   209
        # revert to text version
2c52e4453bf7 api update (incl. doc)
raph
parents: 287
diff changeset
   210
        self.assertEquals(Text.objects.get(pk=1).get_versions_number() , 2)
2c52e4453bf7 api update (incl. doc)
raph
parents: 287
diff changeset
   211
2c52e4453bf7 api update (incl. doc)
raph
parents: 287
diff changeset
   212
        resource = Resource(TextVersionRevertHandler)
2c52e4453bf7 api update (incl. doc)
raph
parents: 287
diff changeset
   213
        request = HttpRequest()
2c52e4453bf7 api update (incl. doc)
raph
parents: 287
diff changeset
   214
        request.method = 'POST'
2c52e4453bf7 api update (incl. doc)
raph
parents: 287
diff changeset
   215
        setattr(request, 'POST' , {})
2c52e4453bf7 api update (incl. doc)
raph
parents: 287
diff changeset
   216
        
2c52e4453bf7 api update (incl. doc)
raph
parents: 287
diff changeset
   217
        response = resource(request, key='text_key_1', version_key='textversion_key_0', emitter_format='json')
2c52e4453bf7 api update (incl. doc)
raph
parents: 287
diff changeset
   218
2c52e4453bf7 api update (incl. doc)
raph
parents: 287
diff changeset
   219
        self.assertEquals(Text.objects.get(pk=1).get_versions_number() , 3)
2c52e4453bf7 api update (incl. doc)
raph
parents: 287
diff changeset
   220
        
2c52e4453bf7 api update (incl. doc)
raph
parents: 287
diff changeset
   221
        # delete text version
2c52e4453bf7 api update (incl. doc)
raph
parents: 287
diff changeset
   222
2c52e4453bf7 api update (incl. doc)
raph
parents: 287
diff changeset
   223
        resource = Resource(TextVersionDeleteHandler)
2c52e4453bf7 api update (incl. doc)
raph
parents: 287
diff changeset
   224
        request = HttpRequest()
2c52e4453bf7 api update (incl. doc)
raph
parents: 287
diff changeset
   225
        request.method = 'POST'
2c52e4453bf7 api update (incl. doc)
raph
parents: 287
diff changeset
   226
        setattr(request, 'POST' , {})
2c52e4453bf7 api update (incl. doc)
raph
parents: 287
diff changeset
   227
        response = resource(request, key='text_key_1', version_key='textversion_key_0', emitter_format='json')
2c52e4453bf7 api update (incl. doc)
raph
parents: 287
diff changeset
   228
        
2c52e4453bf7 api update (incl. doc)
raph
parents: 287
diff changeset
   229
        
2c52e4453bf7 api update (incl. doc)
raph
parents: 287
diff changeset
   230
        self.assertEquals(Text.objects.get(pk=1).get_versions_number() , 2)