~widelands-dev/widelands-website/django_staticfiles

« back to all changes in this revision

Viewing changes to wlmaps/tests/test_views.py

  • Committer: Holger Rapp
  • Date: 2009-02-20 12:25:18 UTC
  • Revision ID: holgerrapp@gmx.net-20090220122518-feaq34ta973snnct
Imported wikiapp into our repository, because we did some local changes (users must be logged in to edit wiki pages)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python -tt
2
 
# encoding: utf-8
3
 
#
4
 
 
5
 
from django.test import TestCase as DjangoTest, Client
6
 
from django.urls import reverse
7
 
from django.contrib.auth.models import User
8
 
# TODO(Franku): Not used, but should be replaced with python json because it gets removed in django 1.7
9
 
#from django.utils import simplejson as json
10
 
from wlmaps.models import *
11
 
 
12
 
import os
13
 
 
14
 
from settings import MEDIA_ROOT
15
 
 
16
 
elven_forests = os.path.dirname(__file__) + '/data/Elven Forests.wmf'
17
 
 
18
 
###########
19
 
# Helpers #
20
 
###########
21
 
 
22
 
 
23
 
class _LoginToSite(DjangoTest):
24
 
 
25
 
    def setUp(self):
26
 
        u = User.objects.create(username='root', email='root@root.com')
27
 
        u.set_password('root')
28
 
        u.save()
29
 
 
30
 
        self.client.login(username='root', password='root')
31
 
 
32
 
        self.user = u
33
 
 
34
 
#############
35
 
# TestCases #
36
 
#############
37
 
###########
38
 
# Uploads #
39
 
###########
40
 
 
41
 
 
42
 
class TestWLMaps_ValidUpload_ExceptCorrectResult(_LoginToSite):
43
 
 
44
 
    def runTest(self):
45
 
        url = reverse('wlmaps_upload')
46
 
        c = self.client.post(
47
 
            url, {'file': open(elven_forests, 'rb'), 'test': True})
48
 
 
49
 
        o = Map.objects.get(pk=1)
50
 
        self.assertEqual(o.name, 'Elven Forests')
51
 
        self.assertEqual(o.author, 'Winterwind')
52
 
 
53
 
 
54
 
class TestWLMaps_AnonUpload_ExceptRedirect(DjangoTest):
55
 
 
56
 
    def runTest(self):
57
 
        url = reverse('wlmaps_upload')
58
 
        k = self.client.post(url, {'file': open(elven_forests, 'rb')})
59
 
        self.assertRedirects(k, reverse(
60
 
            'django.contrib.auth.views.login') + '?next=%s' % url)
61
 
 
62
 
 
63
 
# Invalid Uploading
64
 
class TestWLMaps_UploadWithoutMap_ExceptError(_LoginToSite):
65
 
 
66
 
    def runTest(self):
67
 
        url = reverse('wlmaps_upload')
68
 
        k = self.client.post(url, {'test': True})
69
 
        self.assertEqual(len(Map.objects.all()), 0)
70
 
 
71
 
 
72
 
class TestWLMaps_UploadTwice_ExceptCorrectResult(_LoginToSite):
73
 
 
74
 
    def runTest(self):
75
 
        url = reverse('wlmaps_upload')
76
 
        self.client.post(
77
 
            url, {'file': open(elven_forests, 'rb'), 'test': True})
78
 
        self.assertEqual(len(Map.objects.all()), 1)
79
 
 
80
 
        self.client.post(
81
 
            url, {'file': open(elven_forests, 'rb'), 'test': True})
82
 
        self.assertEqual(len(Map.objects.all()), 1)
83
 
 
84
 
 
85
 
class TestWLMaps_UploadWithInvalidMap_ExceptError(_LoginToSite):
86
 
 
87
 
    def runTest(self):
88
 
        url = reverse('wlmaps_upload')
89
 
        self.client.post(url, {'file': open(__file__, 'rb'), 'test': True})
90
 
        self.assertEqual(len(Map.objects.all()), 0)
91
 
 
92
 
# Viewing
93
 
 
94
 
 
95
 
class TestWLMapsViews_Viewing(DjangoTest):
96
 
 
97
 
    def setUp(self):
98
 
        self.user = User.objects.create(username='testuser')
99
 
        self.user.save()
100
 
 
101
 
        # Add maps
102
 
        nm = Map.objects.create(
103
 
            name='Map',
104
 
            author='Author',
105
 
            w=128,
106
 
            h=64,
107
 
            nr_players=4,
108
 
            descr='a good map to play with',
109
 
            minimap='/wlmaps/minimaps/Map.png',
110
 
            world_name='blackland',
111
 
 
112
 
            uploader=self.user,
113
 
            uploader_comment='Rockdamap'
114
 
        )
115
 
        nm.save()
116
 
        self.map = nm
117
 
        nm = Map.objects.create(
118
 
            name='Map with a long slug',
119
 
            author='Author Paul',
120
 
            w=128,
121
 
            h=64,
122
 
            nr_players=4,
123
 
            descr='a good map to play with',
124
 
            minimap='/wlmaps/minimaps/Map with long slug.png',
125
 
            world_name='blackland',
126
 
 
127
 
            uploader=self.user,
128
 
            uploader_comment='Rockdamap'
129
 
        )
130
 
        nm.save()
131
 
        self.map1 = nm
132
 
 
133
 
    def test_ViewingValidMap_ExceptCorrectResult(self):
134
 
        c = self.client.get(
135
 
            reverse('wlmaps_view', args=('map-with-a-long-slug',)))
136
 
        self.assertEqual(c.status_code, 200)
137
 
        self.assertEqual(c.context['object'], Map.objects.get(
138
 
            slug='map-with-a-long-slug'))
139
 
        self.assertTemplateUsed(c, 'wlmaps/map_detail.html')
140
 
 
141
 
    def test_ViewingNonExistingMap_Except404(self):
142
 
        c = self.client.get(
143
 
            reverse('wlmaps_view', args=('a-map-that-doesnt-exist',)))
144
 
        self.assertEqual(c.status_code, 404)
145