summaryrefslogtreecommitdiff
path: root/testing/tests/server/test_incoming_flow_integration.py
blob: b492534f77045f411355d92362847d47b472b560 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# -*- coding: utf-8 -*-
# test_incoming_flow_integration.py
# Copyright (C) 2017 LEAP
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Integration tests for the complete flow of IncomingBox feature
"""
import pytest
from uuid import uuid4
from twisted.trial import unittest
from twisted.web.server import Site
from twisted.internet import reactor
from twisted.internet import defer
from twisted.web.resource import Resource
from zope.interface import implementer

from leap.soledad.client.incoming import IncomingBoxProcessingLoop
from leap.soledad.client.incoming import IncomingBox
from leap.soledad.server import _blobs as server_blobs
from leap.soledad.client._db.blobs import BlobManager
from leap.soledad.server._incoming import IncomingResource
from leap.soledad.server._blobs import BlobsServerState
from leap.soledad.client import interfaces


@implementer(interfaces.IIncomingBoxConsumer)
class GoodConsumer(object):
    def __init__(self):
        self.name = 'GoodConsumer'
        self.processed, self.saved = [], []

    def process(self, item, item_id, encrypted=True):
        self.processed.append(item_id)
        return defer.succeed([item_id])

    def save(self, parts, item_id):
        self.saved.append(item_id)
        return defer.succeed(None)


class IncomingFlowIntegrationTestCase(unittest.TestCase):

    def setUp(self):
        root = Resource()
        state = BlobsServerState('filesystem', blobs_path=self.tempdir)
        incoming_resource = IncomingResource(state)
        blobs_resource = server_blobs.BlobsResource("filesystem", self.tempdir)
        root.putChild('blobs', blobs_resource)
        root.putChild('incoming', incoming_resource)
        site = Site(root)
        self.port = reactor.listenTCP(0, site, interface='127.0.0.1')
        self.host = self.port.getHost()
        self.uri = 'http://%s:%s/' % (self.host.host, self.host.port)
        self.blobs_uri = self.uri + 'blobs/'
        self.incoming_uri = self.uri + 'incoming'
        self.user_id = 'user-' + uuid4().hex
        self.secret = 'A' * 96
        self.blob_manager = BlobManager(self.tempdir, self.blobs_uri,
                                        self.secret, self.secret,
                                        self.user_id)
        self.box = IncomingBox(self.blob_manager, 'MX')
        self.loop = IncomingBoxProcessingLoop(self.box)
        # FIXME: We use blob_manager client only to avoid DelayedCalls
        # Somehow treq being used here keeps a connection pool open
        self.client = self.blob_manager._client

    def fill(self, messages):
        deferreds = []
        for message_id, message in messages:
            uri = '%s/%s/%s' % (self.incoming_uri, self.user_id, message_id)
            deferreds.append(self.blob_manager._client.put(uri, data=message))
        return defer.gatherResults(deferreds)

    def tearDown(self):
        self.port.stopListening()
        self.blob_manager.close()

    @defer.inlineCallbacks
    @pytest.mark.usefixtures("method_tmpdir")
    def test_consume_a_incoming_message(self):
        yield self.fill([('msg1', 'blob')])
        consumer = GoodConsumer()
        self.loop.add_consumer(consumer)
        yield self.loop()
        self.assertIn('msg1', consumer.processed)