Fakultas Ilmu Komputer UI

Commit 65435691 authored by arif teguh wangi's avatar arif teguh wangi
Browse files

[RED] membuat test hasil citasi pada context

parent f800a23e
......@@ -164,970 +164,14 @@ class DetailMateriTest(TestCase):
response = self.client.get(self.url)
self.assertContains(response, 'Citate APA')
class PostsViewTest(TestCase):
@classmethod
def generate_posts_data(cls, user):
POST_COUNT = 3
COMMENTS_COUNT_PER_POST = [1, 0, 3]
assert POST_COUNT == len(COMMENTS_COUNT_PER_POST)
sample_file = SimpleUploadedFile("Test.jpg", b"Test file")
sample_category = Category.objects.create(name="Test Category")
post_comment_group_dict = {}
for _ in range(POST_COUNT):
post = Materi.objects.create(
uploader=user,
cover=sample_file,
content=sample_file,
)
post.categories.add(sample_category)
post_comment_group_dict[post.id] = {
"data": post,
"comments": [],
}
for i, post_id in enumerate(post_comment_group_dict):
post = post_comment_group_dict[post_id]["data"]
for _ in range(COMMENTS_COUNT_PER_POST[i]):
comment = Comment.objects.create(materi=post)
post_comment_group_dict[post_id]["comments"].append(comment)
# order by latest (-timestamp)
post_comment_group_dict[post_id]["comments"].reverse()
return post_comment_group_dict
@classmethod
def setUpTestData(cls):
cls.url = '/posts/'
cls.user_credentials = {
"email": "user@email.com",
"password": "justpass"
}
cls.user = User.objects.create_user(**cls.user_credentials, is_contributor=True)
cls.data = cls.generate_posts_data(cls.user)
def _request_as_user(self):
self.client.login(**self.user_credentials)
return self.client.get(self.url)
def test_url_resolves_to_posts_view(self):
found = resolve(self.url)
self.assertEqual(found.func.__name__, PostsView.as_view().__name__)
def test_returns_200_on_authenticated_access(self):
response = self._request_as_user()
self.assertEqual(response.status_code, 200)
def test_returns_403_on_unauthenticated_access(self):
response = self.client.get(self.url)
self.assertRaises(PermissionDenied)
self.assertEqual(response.status_code, 403)
def test_returns_correct_template(self):
response = self._request_as_user()
self.assertTemplateUsed(response, "user_uploaded_posts.html")
def test_success_returns_correct_comment_post_groupings_by_context(self):
post_comment_group_dict = self.data
response = self._request_as_user()
response_user = response.context_data["user"]
self.assertEqual(response_user, self.user)
response_data = response.context_data["posts"]
actual_data = post_comment_group_dict
self.assertDictEqual(response_data, actual_data)
def test_html_contains_grouped_posts_and_comments(self):
response = self._request_as_user()
posts = list(self.data.keys())
comments = {
i: [comment.id for comment in self.data[post_id]["comments"]]
for i, post_id in enumerate(posts)
}
self.assertRegex(
str(response.content),
rf'.*(<div id="post-{posts[2]}">)' + \
rf'.*(<div id="post-{posts[2]}-comment-{comments[2][0]}">)' + \
rf'.*(<div id="post-{posts[2]}-comment-{comments[2][1]}">)' + \
rf'.*(<div id="post-{posts[2]}-comment-{comments[2][2]}">)' + \
rf'.*(<div id="post-{posts[1]}">)' + \
rf'.*(<div id="post-{posts[0]}">)' + \
rf'.*(<div id="post-{posts[0]}-comment-{comments[0][0]}">)'
)
class TemplateLoaderTest(TestCase):
def test_template_loader_url_exist(self):
url = "/test-page.html"
response = Client().get(url)
self.assertEqual(response.status_code, 200)
def test_template_loader_using_template_loader_template(self):
url = "/test-page.html"
response = Client().get(url)
expected_template_name = "test-page.html"
self.assertTemplateUsed(response, expected_template_name)
def test_template_loader_using_template_loader_func(self):
url = "/test-page.html"
found = resolve(url)
expected_view_name = "pages"
self.assertEqual(found.func.__name__, expected_view_name)
def test_template_loader_handle_non_existent_html(self):
url = "/test.html"
expected_template_name = "error-404.html"
response = Client().get(url)
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, expected_template_name)
class UploadPageTest(TestCase):
def setUp(self):
self.client = Client()
self.user = User.objects._create_user(email="kontributor@gov.id",
password="kontributor", is_contributor=True)
def test_upload_page_using_login_func(self):
found = resolve("/unggah/")
self.assertEqual(found.func.__name__,
UploadMateriView.as_view().__name__)
def test_uplaod_page_url_is_exist(self):
# Positive test
self.client.login(email="kontributor@gov.id",
password="kontributor")
response = self.client.get("/unggah/")
self.assertEqual(response.status_code, 200)
# Negative tests
self.client.login(email="kontributor@gov.id",
password="kontributor")
response = Client().get("/fake/")
self.assertEqual(response.status_code, 404)
def test_upload_page_template(self):
url = "/unggah/"
self.client.login(email="kontributor@gov.id",
password="kontributor")
response = self.client.get(url)
expected_template_name = "unggah.html"
self.assertTemplateUsed(response, expected_template_name)
def test_upload_page_title(self):
self.client.login(email="kontributor@gov.id",
password="kontributor")
response = self.client.get("/unggah/")
# Positive tests
self.assertContains(response, "Unggah Materi")
# Negative tests
self.assertNotContains(response, "Anything")
def test_upload_page_form_field(self):
self.client.login(email="kontributor@gov.id",
password="kontributor")
response = self.client.get("/unggah/")
# Positive tests
self.assertContains(response, "title")
# Negative tests
self.assertNotContains(response, "anything")
class DashboardKontributorViewTest(TestCase):
def setUp(self):
self.client = Client()
self.kontributor = User.objects.create_contributor(email="kontributor@gov.id",
password="kontributor")
self.admin = User.objects.create_admin(email="admin@gov.id",
password="admin")
self.url = "/dashboard/"
self.view = DashboardKontributorView
self.template_name = "dashboard.html"
def test_dashboard_kontributor_view(self):
found = resolve(self.url)
self.assertEqual(found.func.__name__, self.view.as_view().__name__)
def test_dashboard_kontributor_template(self):
# Login
self.client.login(email="kontributor@gov.id",
password="kontributor")
# Test
response = self.client.get(self.url)
self.assertTemplateUsed(response, self.template_name)
# Logout
self.client.logout()
def test_dashboard_kontributor_url(self):
# Login
self.client.login(email="kontributor@gov.id",
password="kontributor")
# Test
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
# Logout
self.client.logout()
def test_dashboard_kontributor_access(self):
# Kontributor
# Login
self.client.login(email="kontributor@gov.id",
password="kontributor")
# Test
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
# Logout
self.client.logout()
# Admin
# Login
self.client.login(email="admin@gov.id",
password="admin")
# Test
response = self.client.get(self.url)
self.assertEqual(response.status_code, 403)
# Logout
self.client.logout()
# Anonim
# Test
response = self.client.get(self.url)
self.assertEqual(response.status_code, 403)
class ProfilAdminTest(TestCase):
def setUp(self):
self.client = Client()
self.kontributor = User.objects.create_contributor(email="kontributor@gov.id",
password="kontributor")
self.admin = User.objects.create_admin(email="admin@gov.id",
password="admin")
self.url = "/profil-admin/"
self.view = ProfilAdminView
self.template_name = "profil-admin.html"
def test_profil_Admin_view(self):
found = resolve(self.url)
self.assertEqual(found.func.__name__, self.view.as_view().__name__)
def test_profil_admin_template(self):
# Login
self.client.login(email="admin@gov.id",
password="admin")
# Test
response = self.client.get(self.url)
self.assertTemplateUsed(response, self.template_name)
# Logout
self.client.logout()
def test_profil_admin_url(self):
# Login
response = self.client.get(self.url)
self.assertNotEqual(response.status_code, 200)
self.client.login(email="admin@gov.id",
password="admin")
# Test
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
# Logout
self.client.logout()
class ProfilKontributorTest(TestCase):
def setUp(self):
self.client = Client()
self.kontributor = User.objects.create_contributor(email="kontributor@gov.id",
password="kontributor")
self.admin = User.objects.create_admin(email="admin@gov.id",
password="admin")
self.url = "/profil/"
self.view = ProfilKontributorView
self.template_name = "profil.html"
def test_profil_kontributor_view(self):
found = resolve(self.url)
self.assertEqual(found.func.__name__, self.view.as_view().__name__)
def test_profil_kontributor_template(self):
# Login
self.client.login(email="kontributor@gov.id",
password="kontributor")
# Test
response = self.client.get(self.url)
self.assertTemplateUsed(response, self.template_name)
# Logout
self.client.logout()
def test_profil_kontributor_url(self):
# Login
self.client.login(email="kontributor@gov.id",
password="kontributor")
# Test
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
# Logout
self.client.logout()
def test_profil_kontributor_access(self):
# Kontributor
# Login
self.client.login(email="kontributor@gov.id",
password="kontributor")
# Test
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
# Logout
self.client.logout()
# Admin
# Login
self.client.login(email="admin@gov.id",
password="admin")
# Test
response = self.client.get(self.url)
self.assertEqual(response.status_code, 403)
# Logout
self.client.logout()
# Anonim
# Test
response = self.client.get(self.url)
self.assertEqual(response.status_code, 403)
class SuntingProfilTest(TestCase):
def setUp(self):
self.client = Client()
self.kontributor = User.objects.create_contributor(email="kontributor@gov.id",
password="kontributor")
self.admin = User.objects.create_admin(email="admin@gov.id",
password="admin")
self.url = "/sunting/"
self.view = SuntingProfilView
self.template_name = "sunting.html"
def test_sunting_profile_view(self):
found = resolve(self.url)
self.assertEqual(found.func.__name__, self.view.as_view().__name__)
def test_sunting_profile_template(self):
# Login
self.client.login(email="kontributor@gov.id",
password="kontributor")
# Test
response = self.client.get(self.url)
self.assertTemplateUsed(response, self.template_name)
# Logout
self.client.logout()
def test_sunting_profile_url(self):
# Login
self.client.login(email="kontributor@gov.id",
password="kontributor")
# Test
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
# Logout
self.client.logout()
def test_sunting_profile_access(self):
# Kontributor
# Login
self.client.login(email="kontributor@gov.id",
password="kontributor")
# Test
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
# Logout
self.client.logout()
def test_sunting_profile_access_anonymous(self):
# Test
response = self.client.get(self.url)
self.assertEqual(response.status_code, 403)
def test_sunting_profile_autofocus(self):
form_data = {
"email": "kontributor@gov.id",
"name": "kontributor",
"instansi": "instansi",
"nik": "nik",
"alamat": "alamat",
"nomor_telpon": "123456789",
"twitter": "Twit",
"instagram": "https://instagram.com/test_ig"
}
form = SuntingProfilForm(data=form_data)
# Test
self.assertEqual(form.fields["twitter"].widget.attrs.get("autofocus"), "")
self.assertEqual(form.fields["instagram"].widget.attrs.get("autofocus"), None)
class SuntingProfilAdminTest(TestCase):
def setUp(self):
self.client = Client()
self.kontributor = User.objects.create_contributor(email="kontributor@gov.id",
password="kontributor")
self.admin = User.objects.create_admin(email="admin@gov.id",
password="admin")
self.url = "/sunting-admin/"
self.view = SuntingProfilAdminView
self.template_name = "sunting_admin.html"
def test_sunting_profile_admin_view(self):
found = resolve(self.url)
self.assertEqual(found.func.__name__, self.view.as_view().__name__)
def test_sunting_profile_admin_template(self):
# Login
self.client.login(email="admin@gov.id",
password="admin")
# Test
response = self.client.get(self.url)
self.assertTemplateUsed(response, self.template_name)
# Logout
self.client.logout()
def test_sunting_profile_admin_url(self):
# Login
self.client.login(email="admin@gov.id",
password="admin")
# Test
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
# Logout
self.client.logout()
def test_sunting_profile_admin_access(self):
# Kontributor
# Login
self.client.login(email="admin@gov.id",
password="admin")
# Test
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
# Logout
self.client.logout()
class SuksesLoginKontributorTest(TestCase):
def setUp(self):
self.client = Client()
self.kontributor = User.objects.create_contributor(email="kontributor@gov.id",
password="kontributor")
self.admin = User.objects.create_admin(email="admin@gov.id",
password="admin")
self.url = "/sukses-kontributor/"
self.view = SuksesLoginKontributorView
self.template_name = "sukses_kontri.html"
def test_sukses_login_kontributor_view(self):
found = resolve(self.url)
self.assertEqual(found.func.__name__, self.view.as_view().__name__)
def test_sukses_login_kontributor_template(self):
# Login
self.client.login(email="kontributor@gov.id",
password="kontributor")
# Test
response = self.client.get(self.url)
self.assertTemplateUsed(response, self.template_name)
# Logout
self.client.logout()
def test_sukses_login_kontributor_url(self):
# Login
self.client.login(email="kontributor@gov.id",
password="kontributor")
# Test
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
# Logout
self.client.logout()
def test_sukses_login_kontributor_access(self):
# Kontributor
# Login
self.client.login(email="kontributor@gov.id",
password="kontributor")
# Test
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
# Logout
self.client.logout()
class SuksesLoginAdminTest(TestCase):
def setUp(self):
self.client = Client()
self.kontributor = User.objects.create_contributor(email="kontributor@gov.id",
password="kontributor")
self.admin = User.objects.create_admin(email="admin@gov.id",
password="admin")
self.url = "/sukses-admin/"
self.view = SuksesLoginAdminView
self.template_name = "sukses_admin.html"
def test_sukses_login_admin_view(self):
found = resolve(self.url)
self.assertEqual(found.func.__name__, self.view.as_view().__name__)
def test_sukses_login_admin_template(self):
# Login
self.client.login(email="admin@gov.id",
password="admin")
# Test
response = self.client.get(self.url)
self.assertTemplateUsed(response, self.template_name)
# Logout
self.client.logout()
def test_sukses_login_admin_url(self):
# Login
self.client.login(email="admin@gov.id",
password="admin")
# Test
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
# Logout
self.client.logout()
def test_sukses_login_admin_access(self):
# Kontributor
# Login
self.client.login(email="admin@gov.id",
password="admin")
# Test
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
# Logout
self.client.logout()
class LikeMateriTest(TestCase):
def setUp(self):
self.contributor_credential = {
"email": "kontributor@gov.id",
"password": id_generator()
}
self.contributor = get_user_model().objects.create_user(
**self.contributor_credential, name="Kontributor", is_contributor=True
)
self.client = Client()
self.url_like = '/materi/like/'
content = b"Test file"
self.cover = SimpleUploadedFile(
"cover.jpg",
content
)
self.content = SimpleUploadedFile(
"content.txt",
content
)
Materi(title="Materi 1", author="Agas", uploader=self.contributor,
publisher="Kelas SC", descriptions="Deskripsi Materi 1",
status="PENDING", cover=self.cover, content=self.content).save()
self.materi1 = Materi.objects.first()
self.url_materi = f'/materi/{self.materi1.id}/'
def test_get_method(self):
response = self.client.get(self.url_like)
response = json.loads(response.content)
self.assertEqual(response.get("success", None), False)
def test_like_materi(self):
# Verify that materi doesn't have any like to start with
num_of_likes = Like.objects.filter(materi = self.materi1).count()
self.assertEqual(num_of_likes, 0)
# Like a materi
response = self.client.get(self.url_materi)
session_id = response.context["session_id"]
materi_id = response.context["materi_data"].id
payload = {
'materi_id': materi_id,
'session_id': session_id
}
ajax_response = Client().post(self.url_like, payload)
num_of_likes = Like.objects.filter(materi = self.materi1).count()
self.assertEqual(num_of_likes, 1)
def test_unlike_materi(self):
# Verify that materi doesn't have any like to start with
num_of_likes = Like.objects.filter(materi = self.materi1).count()