Fakultas Ilmu Komputer UI

tests.py 84.6 KB
Newer Older
2001
2002
2003
2004
2005
2006
2007
2008
        self.assertEqual(response.status_code, 200)

    def test_download_history_using_correct_template(self):
        response = self.client.get(self.history_url)
        self.assertTemplateUsed(response, "download_history.html")

    def test_download_history_has_user_name(self):
        response = self.client.get(self.history_url)
2009
        resp_html = response.content.decode("utf8")
2010
        self.assertIn(self.user_anonim.name, resp_html)
2011

2012
2013
2014
2015
    def test_registered_user_download_history_correctly_displayed(self):
        self.client.get(self.download_url1)
        self.client.get(self.download_url2)
        self.client.get(self.download_url1)
2016

2017
        jkt_timezone = timezone(TIME_ZONE)
2018

2019
2020
        download_history = self.user_anonim.riwayat_unduh.all()
        response = self.client.get(self.history_url)
2021
        resp_html = response.content.decode("utf8")
2022
2023
2024
2025
        for riwayat in download_history:
            downloaded_materi = riwayat.materi
            self.assertIn(downloaded_materi.title, resp_html)
            self.assertIn(downloaded_materi.author, resp_html)
2026

2027
2028
            jkt_timestamp = riwayat.timestamp.astimezone(jkt_timezone)
            self.assertIn(jkt_timestamp.strftime("%d %B %Y %H:%M:%S"), resp_html)
2029

2030
2031
2032
2033
2034
2035
    def test_unregistered_user_download_history_correctly_displayed(self):
        self.client.logout()

        self.client.get(self.download_url1)
        self.client.get(self.download_url2)
        self.client.get(self.download_url1)
2036

2037
        jkt_timezone = timezone(TIME_ZONE)
2038

2039
        response = self.client.get(self.history_url)
2040
2041
        resp_html = response.content.decode("utf8")
        for riwayat_id in self.client.session["downloaded_materi"]:
2042
2043
2044
2045
            riwayat = DownloadStatistics.objects.get(pk=riwayat_id)
            downloaded_materi = riwayat.materi
            self.assertIn(downloaded_materi.title, resp_html)
            self.assertIn(downloaded_materi.author, resp_html)
2046

2047
2048
            jkt_timestamp = riwayat.timestamp.astimezone(jkt_timezone)
            self.assertIn(jkt_timestamp.strftime("%d %B %Y %H:%M:%S"), resp_html)
2049

2050
2051
2052
2053
2054
2055
    def test_download_history_not_display_if_user_changed(self):
        self.client.get(self.download_url1)
        self.client.get(self.download_url2)
        self.client.get(self.download_url1)

        self.client.logout()
2056

2057
        jkt_timezone = timezone(TIME_ZONE)
2058

2059
2060
        download_history = self.user_anonim.riwayat_unduh.all()
        response = self.client.get(self.history_url)
2061
        resp_html = response.content.decode("utf8")
2062
2063
2064
2065
        for riwayat in download_history:
            downloaded_materi = riwayat.materi
            self.assertNotIn(downloaded_materi.title, resp_html)
            self.assertNotIn(downloaded_materi.author, resp_html)
2066

2067
2068
            jkt_timestamp = riwayat.timestamp.astimezone(jkt_timezone)
            self.assertNotIn(jkt_timestamp.strftime("%d %B %Y %H:%M:%S"), resp_html)
2069

2070
2071
2072
2073
2074
2075
    def test_unregistered_user_download_history_wont_be_saved_if_user_changes(self):
        self.client.logout()

        self.client.get(self.download_url1)
        self.client.get(self.download_url2)
        self.client.get(self.download_url1)
2076

2077
2078
2079
2080
        self.client.get(self.history_url)

        self.client.login(**self.user_credential)
        self.client.logout()
2081
2082
        self.assertFalse("downloaded_materi" in self.client.session)

2083
    def test_download_history_sorted_by_download_time(self):
2084
        # download with 1 second interval to differ download time
2085
2086
2087
2088
2089
2090
2091
        self.client.get(self.download_url1)
        sleep(1)
        self.client.get(self.download_url2)
        sleep(1)
        self.client.get(self.download_url1)
        sleep(1)
        self.client.get(self.download_url2)
2092

2093
        response = self.client.get(self.history_url)
2094
2095
        resp_html = response.content.decode("utf8")

2096
        table_html = ("<table" + resp_html.split("<table")[1]).split("</table>")[0] + "</table>"
2097
2098
        soup = BeautifulSoup(table_html, "html.parser")
        histories_html = soup.find("tbody").find_all("tr")
2099
        prev_timestamp = None
2100

2101
2102
2103
2104
2105
2106
2107
        for riwayat_html in histories_html:
            materi_data = riwayat_html.find_all("td")
            date_format = "%d %B %Y %H:%M:%S"
            materi_timestamp = datetime.strptime(materi_data[2].get_text(), date_format)
            if prev_timestamp:
                self.assertTrue(prev_timestamp > materi_timestamp)
            prev_timestamp = materi_timestamp
2108

2109
2110
2111
    def test_no_history_display_message(self):
        no_history_msg = "Anda belum mengunduh materi. Silahkan unduh materi yang anda butuhkan"
        response = self.client.get(self.history_url)
2112
        resp_html = response.content.decode("utf8")
2113
2114
2115
2116
2117
        self.assertIn(no_history_msg, resp_html)


class MateriModelTest(TestCase):
    def setUp(self):
2118
2119
        self.contributor = User.objects.create(
            email="kontributor@gov.id", password="passwordtest", name="kontributor", is_contributor=True
2120
2121
        )

2122
        self.cover = SimpleUploadedFile("ExampleCover221.jpg", b"Test file")
2123
2124
        self.content = SimpleUploadedFile("ExampleFile221.pdf", b"Test file")

2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
        self.materi = Materi.objects.create(
            title="Materi 1",
            author="Agas",
            uploader=self.contributor,
            publisher="Kelas SC",
            descriptions="Deskripsi Materi 1",
            status="APPROVE",
            cover=self.cover,
            content=self.content,
            date_modified=datetime.now(),
            date_created=datetime.now(),
        )
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146

    def test_like_count_return_zero_when_there_is_no_like(self):
        self.assertEqual(0, self.materi.like_count)

    def test_like_count_return_right_value_when_there_is_like(self):
        Like.objects.create(timestamp=datetime.now(), materi=self.materi, session_id="dummysessionid1")
        self.assertEqual(1, self.materi.like_count)

        Like.objects.create(timestamp=datetime.now(), materi=self.materi, session_id="dummysessionid2")
        self.assertEqual(2, self.materi.like_count)
For faster browsing, not all history is shown. View entire blame