Fakultas Ilmu Komputer UI

test_views_fasilitas.py 5.92 KB
Newer Older
1
2
import json
from http import HTTPStatus
3
4
from django.test import Client
from django.urls import reverse
5

6
from .test_base import InformasiFasilitasViewTest
7
8

PENJELASAN = 'penjelasan fasilitas'
9

10

11
12
13
class FasilitasRelatedViewTest(InformasiFasilitasViewTest):

    def test_can_get_list_fasilitas(self):
14
        self.create_fasilitas_test()
15
        response = Client().get(
16
            reverse('list-fasilitas', kwargs={'nama_lokasi': self.default_lokasi_name}))
17
18
        self.assertEqual(response.status_code, HTTPStatus.OK)

19
20
21
22
23
24
    def test_can_get_list_fasilitas_json(self):
        fasilitas = self.create_fasilitas_test()
        response = Client().get(
            reverse('list-fasilitas', kwargs={'nama_lokasi': self.default_lokasi_name}))
        content = json.loads(response.content.decode('utf-8'))
        expected_json = {'1': {'id': 1,
25
26
27
28
29
30
31
                               'nama_lokasi': self.default_lokasi_name,
                               'deskripsi': self.default_deskripsi,
                               'creator': self.default_last_name,
                               'like': self.default_like,
                               'dislike': self.default_dislike,
                               'rating': self.default_rating,
                               'tag': ['KR', 'RT'],
Usman Sidiq's avatar
Usman Sidiq committed
32
                               'image': '/media/' + str(fasilitas.image),
33
34
35
                               'is_verified': False}
                         }

36
37
38
39
        time = fasilitas.date_time.strftime("%d-%m-%Y %H:%M:%S")
        expected_json["1"]['date_time'] = time
        self.assertEqual(content, expected_json)

40
41
    def test_cannot_post_list_fasilitas(self):
        response = Client().post(
42
            reverse('list-fasilitas', kwargs={'nama_lokasi': self.lokasi_test_1["name"]}))
43
44
45
46
47
48
49
50
        self.assertEqual(response.status_code, HTTPStatus.METHOD_NOT_ALLOWED)

    def test_get_list_fasilitas_not_found(self):
        response = Client().get(
            reverse('list-fasilitas', kwargs={'nama_lokasi': 'Mall'}))
        self.assertEqual(response.status_code, HTTPStatus.NOT_FOUND)

    def test_can_get_detail_fasilitas(self):
51
52
53
54
55
        self.create_fasilitas_test()
        response = Client().get(reverse('detail-fasilitas',
                                        kwargs={'nama_lokasi': self.default_lokasi_name,
                                                'id': 1}))
        self.assertEqual(response.status_code, HTTPStatus.OK)
56

57
    def test_can_get_detail_fasilitas_json(self):
58
        # TODO
59
60
61
62
        self.create_fasilitas_test()
        response = Client().get(reverse('detail-fasilitas',
                                        kwargs={'nama_lokasi': self.default_lokasi_name,
                                                'id': 1}))
63
64
65
        self.assertEqual(response.status_code, HTTPStatus.OK)

    def test_cannot_post_detail_fasilitas(self):
66
        fasilitas = self.create_fasilitas_test()
67
68
69
        response = Client().post(reverse('detail-fasilitas',
                                         kwargs={'nama_lokasi': self.default_lokasi_name,
                                                 'id': fasilitas.id}))
70
71
72
73
74
75
76
77
        self.assertEqual(response.status_code, HTTPStatus.METHOD_NOT_ALLOWED)

    def test_get_detail_fasilitas_not_found(self):
        response = Client().get(reverse('detail-fasilitas',
                                        kwargs={'nama_lokasi': 'Mall', 'id': 0}))
        self.assertEqual(response.status_code, HTTPStatus.NOT_FOUND)

    def test_can_post_add_fasilitas(self):
78
        client = self.client_user_token()
79
        self.create_lokasi_test()
80
81
        urls = reverse('add-fasilitas',
                       kwargs={'nama_lokasi': self.default_lokasi_name})
82
        response = client.post(urls, {'deskripsi': PENJELASAN,
83
                                      'rating': 2,
Usman Sidiq's avatar
Usman Sidiq committed
84
                                      'tag': 'KR RT', 'image': 'test.jpg'})
85
86
87
        self.assertEqual(response.status_code, HTTPStatus.CREATED)

    def test_cannot_get_add_fasilitas(self):
88
89
        client = self.client_user_token()
        self.create_lokasi_test()
90
        response = client.get(
91
            reverse('add-fasilitas', kwargs={'nama_lokasi': self.default_lokasi_name}))
92
93
94
        self.assertEqual(response.status_code, HTTPStatus.METHOD_NOT_ALLOWED)

    def test_post_add_fasilitas_not_found(self):
95
        client = self.client_user_token()
96
        urls = reverse('add-fasilitas', kwargs={'nama_lokasi': 'Mall'})
97
        response = client.post(urls, {'deskripsi': PENJELASAN,
98
99
100
                                      'rating': 2})
        self.assertEqual(response.status_code, HTTPStatus.NOT_FOUND)

101
102
103
104
105
    def test_post_add_fasilitas_cannot_authorized(self):
        client = Client()
        self.create_lokasi_test()
        urls = reverse('add-fasilitas',
                       kwargs={'nama_lokasi': self.default_lokasi_name})
106
        response = client.post(urls, {'deskripsi': PENJELASAN,
107
108
109
110
                                      'rating': 2,
                                      'tag': 'KR RT'})
        self.assertEqual(response.status_code, HTTPStatus.UNAUTHORIZED)

111
    def test_put_update_detail_fasilitas_success(self):
112
113
        client = self.client_user_token()
        self.create_lokasi_test()
114
115
        urls_post = reverse(
            'add-fasilitas', kwargs={'nama_lokasi': self.default_lokasi_name})
116
        data_post = {'deskripsi': PENJELASAN,
117
118
119
120
121
122
123
124
125
126
127
128
129
130
                     'rating': 2,
                     'tag': 'KR RT'}
        response = client.post(urls_post, data=data_post)

        response_json = json.loads(response.content.decode("utf-8"))
        fasilitas_id = response_json["id"]
        urls_put = reverse('update-fasilitas',
                           kwargs={'nama_lokasi': 'Mall', "id": fasilitas_id})
        send_data = {'deskripsi': 'penjelasan fasilitases',
                     'rating': 3,
                     'tag': 'KR'}
        response = client.put(urls_put, data=send_data,
                              content_type="multipart/form-data")
        self.assertEqual(response.status_code, HTTPStatus.ACCEPTED)