~milo/linaro-ci-dashboard/refactor-job-schedule

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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env python
# Copyright (C) 2012 Linaro
#
# This file is part of linaro-ci-dashboard.
#
# linaro-ci-dashboard is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# linaro-ci-dashboard 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with linaro-ci-dashboard.  If not, see <http://www.gnu.org/licenses/>.

from django.test import TestCase
from django.contrib.auth.models import User, Group
from django.test.client import Client
import urllib


class ClientResponseTestsGeneral(TestCase):
    def setUp(self):
        self.client = Client()
        self.user = 'someuser'
        self.passwd = 'somepasswd'
        self.email = 'someuser@example.com'
        self.group = 'somegroup'
        self.user = User.objects.create_user(self.user,
                                             self.email, self.passwd)
        self.group = Group.objects.create(name=self.group)

    def tearDown(self):
        user = User.objects.filter(username=self.user)
        group = Group.objects.filter(name=self.group)
        if user:
            user.delete()
        if group:
            group.delete()


class ClientResponseTests(ClientResponseTestsGeneral):

    def setUp(self):
        super(ClientResponseTests, self).setUp()
        self.integration_loop_data = {"name" : "testjob",
                                      "branch" : "lp:linaro-ci-dashboard",
                                      "precommand" : "make migrate",
                                      "command" : "make test",
                                      }

    # Test case should fail if the login_required decorator source code
    # in index_view.py is removed.
    def test_response_index_when_user_not_auth_by_login_required(self):
        response = self.client.get('/index/')
        self.assertNotEqual(200, response.status_code)

    def test_response_home_when_user_not_auth_by_login_required(self):
        response = self.client.get('/home/')
        self.assertNotIn(str(self.user), response.content)

    def test_response_with_invalid_user(self):
        passwd = 'invalid_passwd'
        login_value = self.client.login(username=self.user, password=passwd)
        self.assertEqual(login_value, False)

    def test_response_when_user_authed(self):
        self.client.login(username=self.user, password=self.passwd)
        response = self.client.get('/')
        self.assertIn(str(self.user), response.content)
        response = self.client.get('/home/')
        self.assertIn(str(self.user), response.content)

    def test_integration_loop_create_get(self):
        self.client.login(username=self.user, password=self.passwd)
        response = self.client.get("/integration_loop/create/")
        self.assertIn("/integration_loop/create/", response.content)
        template_names = [template.name for template in
                          response.templates]
        self.assertEquals(template_names, ["integration_loop_create.html",
                                           "base.html",
                                           "login.html",
                                           ])

    def test_integration_loop_create_post_no_data(self):
        self.client.login(username=self.user, password=self.passwd)
        response = self.client.post("/integration_loop/create/", {})
        self.assertEquals(response.status_code, 200)
        self.assertIn("This field is required", response.content)

    def test_integration_loop_create_post(self):
        self.client.login(username=self.user, password=self.passwd)

        # Follow redirect after save.
        response = self.client.post("/integration_loop/create/",
                                    self.integration_loop_data, follow=True)
        # Assert response code.
        self.assertEquals(response.status_code, 200)
        # Assert redirect chain.
        self.assertEquals(response.redirect_chain[0][1], 302)
        self.assertIn(urllib.quote(self.integration_loop_data["name"]),
                      response.redirect_chain[0][0])
        # Assert template names for detail page.
        template_names = [template.name for template in
                          response.templates]
        self.assertEquals(template_names, ["integration_loop_detail.html",
                                           "base.html",
                                           "login.html",
                                           ])
        # Assert if everything is present on detail page.
        self.assertIn(self.integration_loop_data["name"], response.content)
        self.assertIn(self.integration_loop_data["branch"], response.content)
        self.assertIn(self.integration_loop_data["precommand"],
                      response.content)
        self.assertIn(self.integration_loop_data["command"], response.content)