Fakultas Ilmu Komputer UI

test_views_komentar.py 2.92 KB
Newer Older
1
2
import json
from http import HTTPStatus
3
from django.urls import reverse
4
5

from .test_base import InformasiFasilitasViewTest
6
7
8
from .models import Komentar

COMMENT = "halo saya komen"
9
10
11
12
13
14
15
16
17
18
19


class KomentarRelatedViewTest(InformasiFasilitasViewTest):

    def test_can_comment_facility(self):
        client = self.client_user_token()
        fasilitas = self.create_fasilitas_test()
        urls = reverse('add-komentar',
                       kwargs={'nama_lokasi': self.default_lokasi_name,
                               'id': 1,
                               })
20
        comment_data = {"deskripsi": COMMENT}
21
22
23
24
25
26
27
28
29
30
31
        client.post(urls, comment_data)
        count = Komentar.objects.filter(fasilitas=fasilitas).count()
        self.assertEqual(count, 1)

    def test_can_comment_facility_status_code(self):
        client = self.client_user_token()
        self.create_fasilitas_test()
        urls = reverse('add-komentar',
                       kwargs={'nama_lokasi': self.default_lokasi_name,
                               'id': 1,
                               })
32
        comment_data = {'deskripsi': COMMENT}
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
        response = client.post(urls, comment_data)
        self.assertEqual(response.status_code, HTTPStatus.CREATED)

    def test_not_comment_facility_empty(self):
        client = self.client_user_token()
        self.create_fasilitas_test()
        urls = reverse('add-komentar',
                       kwargs={'nama_lokasi': self.default_lokasi_name,
                               'id': 1,
                               })
        comment_data = {}
        response = client.post(urls, comment_data)
        self.assertEqual(response.status_code, HTTPStatus.BAD_REQUEST)

    def test_not_comment_facility_empty_response(self):
        client = self.client_user_token()
        self.create_fasilitas_test()
        urls = reverse('add-komentar',
                       kwargs={'nama_lokasi': self.default_lokasi_name,
                               'id': 1,
                               })
        comment_data = {}
        response = client.post(urls, comment_data)
        response_json = json.loads(response.content.decode('utf-8'))
57
        expected_json = {"response": "Bad Request. 'deskripsi' key is needed"}
58
        self.assertEqual(expected_json, response_json)
59
60
61
62
63
64
65
66

    def test_get_list_komentar(self):
        client = self.client_user_token()
        self.create_fasilitas_test()
        urls = reverse('add-komentar',
                       kwargs={'nama_lokasi': self.default_lokasi_name,
                               'id': 1,
                               })
67
        comment_data = {"deskripsi": COMMENT}
68
69
70
71
72
73
74
        client.post(urls, comment_data)
        urls_get = reverse('list-komentar',
                           kwargs={'nama_lokasi': self.default_lokasi_name,
                                   'id': 1,
                                   })
        response = client.get(urls_get)
        self.assertEqual(response.status_code, HTTPStatus.OK)