event_test.py 3.11 KB
Newer Older
Romain Bignon's avatar
Romain Bignon committed
1 2 3
from assnet.storage import Storage
from assnet.server import Server
from assnet.users import User
4 5 6 7 8 9 10 11

from unittest import TestCase
from webtest import TestApp

from tempfile import mkdtemp
import os
import shutil

Laurent Bachelier's avatar
Laurent Bachelier committed
12

13 14
class EventTest(TestCase):
    def setUp(self):
Romain Bignon's avatar
Romain Bignon committed
15
        self.root = mkdtemp(prefix='assnet_test_root')
16
        storage = Storage.create(self.root)
17
        server = Server(self.root)
18
        self.app = TestApp(server)
19

20
        user = User(storage, 'penguin')
21
        user.realname = 'Penguin'
22 23 24 25 26
        user.password = 'monkey1'
        user.save()
        user = User(storage, 'platypus')
        user.realname = 'Platypus'
        user.password = 'passw0rd'
27 28 29 30 31 32 33 34 35 36 37 38 39 40
        user.save()

        event_text = """Reunion
---

Reunion of the penguins

Date:
2001-02-03 13:37

Place:
Somewhere

Attendees:
41 42
[ ] penguin (Penguin)
[x] platypus (Platypus)
43
[ ] doesnotexist (Does Not Exist)
44
-- 0 confirmed, 0 waiting, 0 declined
45 46 47 48 49

        """
        with open(os.path.join(self.root, 'event1.txt'), 'w') as f:
            f.write(event_text)

50
        f = storage.get_file('/event1.txt')
51
        f.mimetype = 'text/event'
52
        f.perms['u.penguin'] = f.PERM_READ | f.PERM_WRITE | f.PERM_IN
53 54 55 56 57 58 59 60 61
        f.save()

    def tearDown(self):
        if self.root:
            shutil.rmtree(self.root)

    def test_viewEvent(self):
        res = self.app.get('/event1.txt', status=200)
        assert '<h3>Place</h3>' in res.body
62 63 64
        assert 'Your current status' not in res.body
        assert 'Accept</button>' not in res.body
        assert 'Decline</button>' not in res.body
65 66 67

        res = self.app.get('/?action=login', status=200)
        form = res.form
68 69
        form['login[username]'] = 'penguin'
        form['login[password]'] = 'monkey1'
70 71 72 73 74 75
        res = form.submit(status=302)
        res = res.follow(status=200)

        res = self.app.get('/event1.txt', status=200)
        assert '<h3>Place</h3>' in res.body
        assert '<strong>waiting</strong>' in res.body
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
        assert 'Confirm</button>' in res.body
        assert 'Decline</button>' in res.body

        # decline then accept ('REST' style)
        res = self.app.delete('/event1.txt', status=200)
        assert '<h3>Place</h3>' in res.body
        assert '<strong>declined</strong>' in res.body
        assert 'Confirm</button>' in res.body
        assert 'Decline</button>' not in res.body

        res = self.app.put('/event1.txt', status=200)
        assert '<h3>Place</h3>' in res.body
        assert '<strong>confirmed</strong>' in res.body
        assert 'Confirm</button>' not in res.body
        assert 'Decline</button>' in res.body

        # same but for browsers
        res = self.app.post('/event1.txt', {'_method': 'DELETE'}, status=200)
        assert '<h3>Place</h3>' in res.body
        assert '<strong>declined</strong>' in res.body
        assert 'Confirm</button>' in res.body
        assert 'Decline</button>' not in res.body

        res = self.app.put('/event1.txt', {'_method': 'PUT'}, status=200)
        assert '<h3>Place</h3>' in res.body
        assert '<strong>confirmed</strong>' in res.body
        assert 'Confirm</button>' not in res.body
        assert 'Decline</button>' in res.body