import json
import urllib

from twisted.trial import unittest

import mock

from mlabsim import update


class UpdateResourceTests (unittest.TestCase):

    def test_render_PUT_valid_parameters(self):
        # Test data:
        fqdn = 'mlab01.ooni-tests.not-real.except-it-actually-could-be.example.com'

        tool_extra = {
            'collector_onion': 'testfakenotreal.onion',
            }
        tool_extra_param = urllib.quote(json.dumps(tool_extra))

        # Mocks / components:
        db = {}

        # Mocks:
        m_request = mock.MagicMock()

        # Fake a request with sufficient parameters:
        m_request.params = {
            'fqdn': fqdn,
            'tool_extra': tool_extra_param,
            }

        # Execute the code under test:
        ur = update.UpdateResource(db)
        ur.render_PUT(m_request)

        # Verify that m_db now stores fqdn: tool_extra:
        self.assertEqual({fqdn: {"tool_extra": tool_extra}}, db)




@@ -59,20 +59,72 @@
 est%0A
-%0Aimport mock
+from twisted.web import server%0A%0Afrom mock import MagicMock, call
 %0A%0Afr
@@ -529,21 +529,8 @@
 ocks
- / components
 :%0A  
@@ -542,34 +542,16 @@
  db = %7B%7D
-%0A%0A        # Mocks:
 %0A       
@@ -567,13 +567,8 @@
 t = 
-mock.
 Magi
@@ -828,16 +828,25 @@
 %0A       
+ retval =
  ur.rend
@@ -864,16 +864,96 @@
 quest)%0A%0A
+        # Verifications:%0A        self.assertEqual(server.NOT_DONE_YET, retval)%0A%0A
         
@@ -1066,10 +1066,1387 @@
 %7D, db)%0A%0A
-%0A
+        # Verify that a 200 response was sent:%0A        self.assertEqual(%0A            m_request.mock_calls,%0A            %5Bcall.setResponseCode(200, 'ok'),%0A             call.finish(),%0A             %5D)%0A%0A    def test_render_PUT_malformed_JSON(self):%0A        # Test data:%0A        fqdn = 'mlab01.ooni-tests.not-real.except-it-actually-could-be.example.com'%0A%0A        tool_extra = %7B%0A            'collector_onion': 'testfakenotreal.onion',%0A            %7D%0A        # The slice, %5B1:%5D is to mangle the json:%0A        tool_extra_param = urllib.quote(json.dumps(tool_extra)%5B1:%5D)%0A%0A        # Mocks / components:%0A        m_db = MagicMock()%0A        m_request = MagicMock()%0A%0A        # Fake a request with sufficient parameters:%0A        m_request.params = %7B%0A            'fqdn': fqdn,%0A            'tool_extra': tool_extra_param,%0A            %7D%0A%0A        # Execute the code under test:%0A        ur = update.UpdateResource(m_db)%0A        retval = ur.render_PUT(m_request)%0A%0A        # Verifications:%0A        self.assertEqual(server.NOT_DONE_YET, retval)%0A%0A        # Verify that m_db was not modified (or accessed) in any way:%0A        self.assertEqual(%0A            m_db.mock_calls,%0A            %5B%5D,%0A            )%0A%0A        # Verify that a 400 response was sent:%0A        self.assertEqual(%0A            m_request.mock_calls,%0A            %5Bcall.setResponseCode(400, 'invalid'),%0A             call.finish(),%0A             %5D)
 %0A

Add a new test for invalid JSON and update the old test to verify mock usage with ``.mock_calls``.