def test_get(self):
        base_deed = DeedHelper._create_deed_db()
        deed = Deed.get(base_deed.id)

        self.assertEqual(deed.id, base_deed.id)

        DeedHelper._delete_deed(base_deed.id)
    def test_get(self):
        base_deed = DeedHelper._create_deed_db()
        deed = deed_service.get(base_deed.id)

        self.assertEqual(deed.id, base_deed.id)

        DeedHelper._delete_deed(base_deed.id)
    def test_get_route(self, client):
        deed = DeedHelper._create_deed_db()

        response = client.get('/deed/{}'.format(deed.id))

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertTrue(str(deed.id) in response.data.decode())

        DeedHelper._delete_deed(deed.id)
    def test_get_route(self, client):
        deed = DeedHelper._create_deed_db()

        response = client.get('/deed/{}'.format(deed.id))

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertTrue(str(deed.id) in response.data.decode())

        DeedHelper._delete_deed(deed.id)
    def test_get_by_token(self, client):
        deed = DeedHelper._create_deed_db()

        token = deed.json_doc["operative-deed"]["borrowers"][0]["token"]
        response = client.get('/deed/borrower/{}'.format(token))

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertTrue(str(token) in response.data.decode())

        DeedHelper._delete_deed(deed.id)
    def test_create_generates_token(self, client):
        headers = {'content-type': 'application/json'}
        response = client.post('/deed/', data=json.dumps(DeedHelper._json_doc),
                               headers=headers)

        result = json.loads(response.data.decode())
        get_response = client.get('/deed/{}'.format(result['id']))

        self.assertTrue("token" in get_response.data.decode())

        DeedHelper._delete_deed(result['id'])
    def test_create_generates_token(self, client):
        headers = {'content-type': 'application/json'}
        response = client.post('/deed/', data=json.dumps(DeedHelper._json_doc),
                               headers=headers)

        result = json.loads(response.data.decode())
        get_response = client.get('/deed/{}'.format(result['id']))

        self.assertTrue("token" in get_response.data.decode())

        DeedHelper._delete_deed(result['id'])
    def test_get_by_token(self, client):
        deed = DeedHelper._create_deed_db()

        deed_json = deed.json_doc['deed']
        token = deed_json["operative-deed"]["borrowers"][0]["token"]
        response = client.get('/deed/borrower/{}'.format(token))

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertTrue(str(token) in response.data.decode())

        DeedHelper._delete_deed(deed.id)
    def test_get_deed_by_token(self):
        base_deed = DeedHelper._create_deed_db()

        base_deed_token = \
            base_deed.json_doc["operative-deed"]["borrowers"][0]["token"]

        retrieved_deed_from_token = Deed.get_deed_by_token(base_deed_token)

        self.assertEqual(base_deed.id,
                         retrieved_deed_from_token.id)
        self.assertEqual(base_deed.json_doc,
                         retrieved_deed_from_token.json_doc)

        DeedHelper._delete_deed(base_deed.id)
    def test_sign_route_forbidden(self, client):
        deed = DeedHelper._create_deed_db()

        response = client.get('/deed/{}'.format(deed.id))

        self.assertEqual(response.status_code, status.HTTP_200_OK)

        signature = "@#$%%^&"
        response = client.post('/deed/{}/{}/signature/'
                               .format(deed.id, "10"),
                               data={"signature": signature})

        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

        DeedHelper._delete_deed(deed.id)
    def test_sign_route_forbidden(self, client):
        deed = DeedHelper._create_deed_db()

        response = client.get('/deed/{}'.format(deed.id))

        self.assertEqual(response.status_code, status.HTTP_200_OK)

        signature = "@#$%%^&"
        response = client.post('/deed/{}/{}/signature/'
                               .format(deed.id, "10"),
                               data={"signature": signature})

        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

        DeedHelper._delete_deed(deed.id)
    def test_get_deed_by_token(self):
        base_deed = DeedHelper._create_deed_db()

        operative_deed_ = base_deed.json_doc['deed']["operative-deed"]
        base_deed_token = operative_deed_["borrowers"][0]["token"]

        retrieved_deed_from_token = deed_service.get_deed_by_token(
            base_deed_token
        )

        self.assertEqual(base_deed.id,
                         retrieved_deed_from_token.id)
        self.assertEqual(base_deed.json_doc,
                         retrieved_deed_from_token.json_doc)

        DeedHelper._delete_deed(base_deed.id)
    def test_confirm_completion(self, client):
        deed = DeedHelper._create_deed_db()

        signature = 'SIGNATURE'
        registrars_signature = {
            "registrars-signature": signature
        }
        response = client.post('/deed/{}/completion'.format(deed.id),
                               data=registrars_signature)

        self.assertEqual(response.status_code, status.HTTP_200_OK)

        get_response = client.get('/deed/{}'.format(deed.id))

        deed_json = get_response.data.decode()
        self.assertIn('"registrars-signature": "{}"'.format(signature),
                      deed_json)

        DeedHelper._delete_deed(deed.id)
    def test_delete(self):
        base_deed = DeedHelper._create_deed_db()
        deed = deed_service.get(base_deed.id)

        self.assertEqual(deed.id, DeedHelper._id)

        deed_service.delete(deed.id)
        deed = deed_service.get(deed.id)

        self.assertIs(deed, None)
    def test_confirm_completion(self, client):
        deed = DeedHelper._create_deed_db()

        signature = 'SIGNATURE'
        registrars_signature = {
            "registrars-signature": signature
        }
        response = client.post('/deed/{}/completion'.format(deed.id),
                               data=registrars_signature)

        self.assertEqual(response.status_code, status.HTTP_200_OK)

        get_response = client.get('/deed/{}'.format(deed.id))

        deed_json = get_response.data.decode()
        self.assertIn('"registrars-signature": "{}"'.format(signature),
                      deed_json)

        DeedHelper._delete_deed(deed.id)
    def test_delete(self):
        base_deed = DeedHelper._create_deed_db()
        deed = Deed.get(base_deed.id)

        self.assertEqual(deed.id, DeedHelper._id)

        Deed.delete(deed.id)
        deed = Deed.get(deed.id)

        self.assertIs(deed, None)
    def test_sign_route(self, client):
        deed = DeedHelper._create_deed_db()

        response = client.get('/deed/{}'.format(deed.id))

        self.assertEqual(response.status_code, status.HTTP_200_OK)

        signature = "@#$%%^&"
        payload = json.dumps({
            "signature": signature
        })
        response = client.post(
            '/deed/{}/{}/signature/'.format(deed.id, "1"),
            data=payload,
            headers={'content-type': 'application/json'}
        )

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertTrue(str(signature) in response.data.decode())

        DeedHelper._delete_deed(deed.id)
    def test_delete_route(self, client):
        deed = DeedHelper._create_deed_db()

        response = client.get('/deed/{}'.format(deed.id))

        self.assertEqual(response.status_code, status.HTTP_200_OK)

        Deed.delete(deed.id)

        response = client.get('/deed/{}'.format(deed.id))

        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
    def test_all_borrowers_signed(self):
        base_deed = DeedHelper._create_deed_db()
        johns_id = 1

        has_john_signed = deed_service.all_borrowers_signed(base_deed)
        self.assertFalse(has_john_signed)

        deed_service.sign_deed(base_deed, johns_id, 'I am John!')
        base_deed.save()

        has_john_signed = deed_service.all_borrowers_signed(base_deed)
        self.assertTrue(has_john_signed)
    def test_all_borrowers_signed(self):
        base_deed = DeedHelper._create_deed_db()
        johns_id = 1

        has_john_signed = base_deed.all_borrowers_signed()
        self.assertFalse(has_john_signed)

        base_deed.sign_deed(johns_id, 'I am John!')
        base_deed.save()

        has_john_signed = base_deed.all_borrowers_signed()
        self.assertTrue(has_john_signed)
    def test_sign_route(self, client):
        deed = DeedHelper._create_deed_db()

        response = client.get('/deed/{}'.format(deed.id))

        self.assertEqual(response.status_code, status.HTTP_200_OK)

        signature = "@#$%%^&"
        payload = json.dumps({
            "signature": signature
        })
        response = client.post(
            '/deed/{}/{}/signature/'.format(deed.id, "1"),
            data=payload,
            headers={'content-type': 'application/json'}
        )

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertTrue(str(signature) in response.data.decode())

        DeedHelper._delete_deed(deed.id)
    def test_delete_route(self, client):
        deed = DeedHelper._create_deed_db()

        response = client.get('/deed/{}'.format(deed.id))

        self.assertEqual(response.status_code, status.HTTP_200_OK)

        deed_service.delete(deed.id)

        response = client.get('/deed/{}'.format(deed.id))

        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
    def test_get_names_signed(self, client):
        deed = DeedHelper._create_deed_db()

        response = client.get('/deed/{}/signed/name'.format(deed.id))
        resp_json = json.loads(response.data)

        self.assertEquals([], resp_json['names'])

        deed_service.sign_deed(deed, 1, "I'm John Smith!")
        deed.save()

        response = client.get('/deed/{}/signed/name'.format(deed.id))
        resp_json = json.loads(response.data)

        self.assertEquals(['John Smith'], resp_json['names'])
    def test_get_names_signed(self, client):
        deed = DeedHelper._create_deed_db()

        response = client.get('/deed/{}/signed/name'.format(deed.id))
        resp_json = json.loads(response.data)

        self.assertEquals([], resp_json['names'])

        deed.sign_deed(1, "I'm John Smith!")
        deed.save()

        response = client.get('/deed/{}/signed/name'.format(deed.id))
        resp_json = json.loads(response.data)

        self.assertEquals(['John Smith'], resp_json['names'])
    def test_names_of_borrowers_signed_and_not(self):
        base_deed = DeedHelper._create_deed_db()
        johns_id = 1

        self.assertListEqual(
            ['John Smith'],
            Deed.names_of_all_borrowers_not_signed(base_deed.id)
        )
        self.assertListEqual(
            [],
            Deed.names_of_all_borrowers_signed(base_deed.id)
        )

        base_deed.sign_deed(johns_id, 'I am John!')
        base_deed.save()

        self.assertListEqual(
            [],
            Deed.names_of_all_borrowers_not_signed(base_deed.id)
        )
        self.assertListEqual(
            ['John Smith'],
            Deed.names_of_all_borrowers_signed(base_deed.id)
        )
    def test_names_of_borrowers_signed_and_not(self):
        base_deed = DeedHelper._create_deed_db()
        johns_id = 1

        self.assertListEqual(
            ['John Smith'],
            deed_service.names_of_all_borrowers_not_signed(base_deed.id)
        )
        self.assertListEqual(
            [],
            deed_service.names_of_all_borrowers_signed(base_deed.id)
        )

        deed_service.sign_deed(base_deed, johns_id, 'I am John!')
        base_deed.save()

        self.assertListEqual(
            [],
            deed_service.names_of_all_borrowers_not_signed(base_deed.id)
        )
        self.assertListEqual(
            ['John Smith'],
            deed_service.names_of_all_borrowers_signed(base_deed.id)
        )