Fakultas Ilmu Komputer UI
Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
PMPL
Class Project
DIGIPUS
Commits
5a5a3ba9
Commit
5a5a3ba9
authored
Oct 09, 2020
by
Azhar Rais
Browse files
[REFACTOR] Refactor duplicate literal
parent
31b4486b
Pipeline
#58018
passed with stages
in 17 minutes and 19 seconds
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
forum/tests.py
View file @
5a5a3ba9
...
...
@@ -14,20 +14,29 @@ from forum.models import Discussion, DiscussionComment
CONTRIBUTOR_EMAIL
=
'kontributor@gov.id'
CONTRIBUTOR_PASSWORD
=
'kontributor'
TEST_EMAIL
=
'test@mail.com'
FORUM_BASE_URL
=
'/forum/'
URL_CREATE_DISCUSSION
=
'/forum/discussion/create'
URL_DISCUSSION_DETAIL
=
'/forum/discussion/'
DESCRIPTION_DEFAULT
=
'Discussion description'
TIME_ZONE_MODULE
=
'django.utils.timezone.now'
MATERI_TITLE_ONE
=
'Materi 1'
MATERI_TITLE_TWO
=
'Materi 2'
COMMENT_ONE
=
'Comment 1'
COMMENT_TWO
=
'Comment 2'
URL_DELETE
=
'/delete'
URL_DELETE_DISCUSSION_COMMENT
=
'{0}{1}/{2}/delete'
COMPLETE_VALID_DATA
=
'Complete valid data'
class
DiscussionModelTest
(
TestCase
):
def
setUp
(
self
):
self
.
DESCRIPTION_DEFAULT
=
'Discussion description'
self
.
USER_CONTRIBUTOR
=
User
.
objects
.
create_contributor
(
email
=
CONTRIBUTOR_EMAIL
,
password
=
CONTRIBUTOR_PASSWORD
)
def
test_create_discussion_without_user_fail
(
self
):
discussion
=
Discussion
()
discussion
.
title
=
'Discussion without user'
discussion
.
description
=
self
.
DESCRIPTION_DEFAULT
discussion
.
description
=
DESCRIPTION_DEFAULT
with
self
.
assertRaises
(
IntegrityError
):
discussion
.
save
()
...
...
@@ -35,7 +44,7 @@ class DiscussionModelTest(TestCase):
def
test_create_discussion_with_user_success
(
self
):
discussion
=
Discussion
()
discussion
.
title
=
'Discussion with user'
discussion
.
description
=
self
.
DESCRIPTION_DEFAULT
discussion
.
description
=
DESCRIPTION_DEFAULT
discussion
.
user
=
self
.
USER_CONTRIBUTOR
self
.
assertEqual
(
0
,
Discussion
.
objects
.
all
().
count
())
...
...
@@ -45,7 +54,7 @@ class DiscussionModelTest(TestCase):
def
test_create_discussion_check_created_at_time_stamp
(
self
):
discussion
=
Discussion
()
discussion
.
title
=
'Discussion check created_at'
discussion
.
description
=
self
.
DESCRIPTION_DEFAULT
discussion
.
description
=
DESCRIPTION_DEFAULT
discussion
.
user
=
self
.
USER_CONTRIBUTOR
mock_time
=
timezone
.
now
()
...
...
@@ -58,14 +67,14 @@ class DiscussionModelTest(TestCase):
def
test_update_discussion_check_updated_at_time_stamp
(
self
):
discussion
=
Discussion
()
discussion
.
title
=
'Discussion before update'
discussion
.
description
=
self
.
DESCRIPTION_DEFAULT
discussion
.
description
=
DESCRIPTION_DEFAULT
discussion
.
user
=
self
.
USER_CONTRIBUTOR
discussion
.
save
()
discussion
.
title
=
'Discussion after update'
mock_time
=
timezone
.
now
()
with
mock
.
patch
(
'django.utils.timezone.now'
,
mock
.
Mock
(
return_value
=
mock_time
)):
with
mock
.
patch
(
TIME_ZONE_MODULE
,
mock
.
Mock
(
return_value
=
mock_time
)):
discussion
.
save
()
self
.
assertEqual
(
discussion
.
updated_at
,
mock_time
)
self
.
assertNotEqual
(
discussion
.
created_at
,
discussion
.
updated_at
)
...
...
@@ -73,7 +82,7 @@ class DiscussionModelTest(TestCase):
def
test_discussion_has_many_to_many_relation_with_materi
(
self
):
discussion
=
Discussion
()
discussion
.
title
=
"Check has many to many relation"
discussion
.
description
=
self
.
DESCRIPTION_DEFAULT
discussion
.
description
=
DESCRIPTION_DEFAULT
discussion
.
user
=
self
.
USER_CONTRIBUTOR
discussion
.
save
()
...
...
@@ -85,15 +94,14 @@ class DiscussionModelTest(TestCase):
class
DiscussionCommentModelTest
(
TestCase
):
def
setUp
(
self
):
self
.
DESCRIPTION_DEFAULT
=
'Discussion description'
self
.
USER_CONTRIBUTOR
=
User
.
objects
.
create_contributor
(
email
=
CONTRIBUTOR_EMAIL
,
password
=
CONTRIBUTOR_PASSWORD
)
self
.
DISCUSSION
=
Discussion
.
objects
.
create
(
title
=
'Discussion'
,
description
=
self
.
DESCRIPTION_DEFAULT
,
self
.
DISCUSSION
=
Discussion
.
objects
.
create
(
title
=
'Discussion'
,
description
=
DESCRIPTION_DEFAULT
,
user
=
self
.
USER_CONTRIBUTOR
)
def
test_create_discussion_comment_without_user_fail
(
self
):
discussion_comment
=
DiscussionComment
()
discussion_comment
.
discussion
=
self
.
DISCUSSION
discussion_comment
.
description
=
self
.
DESCRIPTION_DEFAULT
discussion_comment
.
description
=
DESCRIPTION_DEFAULT
with
self
.
assertRaises
(
IntegrityError
):
discussion_comment
.
save
()
...
...
@@ -101,7 +109,7 @@ class DiscussionCommentModelTest(TestCase):
def
test_create_discussion_comment_without_discussion_thread_fail
(
self
):
discussion_comment
=
DiscussionComment
()
discussion_comment
.
user
=
self
.
USER_CONTRIBUTOR
discussion_comment
.
description
=
self
.
DESCRIPTION_DEFAULT
discussion_comment
.
description
=
DESCRIPTION_DEFAULT
with
self
.
assertRaises
(
IntegrityError
):
discussion_comment
.
save
()
...
...
@@ -110,7 +118,7 @@ class DiscussionCommentModelTest(TestCase):
discussion_comment
=
DiscussionComment
()
discussion_comment
.
user
=
self
.
USER_CONTRIBUTOR
discussion_comment
.
discussion
=
self
.
DISCUSSION
discussion_comment
.
description
=
self
.
DESCRIPTION_DEFAULT
discussion_comment
.
description
=
DESCRIPTION_DEFAULT
self
.
assertEqual
(
0
,
DiscussionComment
.
objects
.
all
().
count
())
discussion_comment
.
save
()
...
...
@@ -118,20 +126,20 @@ class DiscussionCommentModelTest(TestCase):
def
test_create_discussion_comment_check_created_at_time_stamp
(
self
):
discussion_comment
=
DiscussionComment
()
discussion_comment
.
description
=
self
.
DESCRIPTION_DEFAULT
discussion_comment
.
description
=
DESCRIPTION_DEFAULT
discussion_comment
.
user
=
self
.
USER_CONTRIBUTOR
discussion_comment
.
discussion
=
self
.
DISCUSSION
mock_time
=
timezone
.
now
()
with
mock
.
patch
(
'django.utils.timezone.now'
,
mock
.
Mock
(
return_value
=
mock_time
)):
with
mock
.
patch
(
TIME_ZONE_MODULE
,
mock
.
Mock
(
return_value
=
mock_time
)):
discussion_comment
.
save
()
self
.
assertEqual
(
discussion_comment
.
created_at
,
mock_time
)
self
.
assertEqual
(
discussion_comment
.
created_at
,
discussion_comment
.
updated_at
)
def
test_update_discussion_comment_check_updated_at_time_stamp
(
self
):
discussion_comment
=
DiscussionComment
()
discussion_comment
.
description
=
self
.
DESCRIPTION_DEFAULT
discussion_comment
.
description
=
DESCRIPTION_DEFAULT
discussion_comment
.
user
=
self
.
USER_CONTRIBUTOR
discussion_comment
.
discussion
=
self
.
DISCUSSION
...
...
@@ -139,14 +147,14 @@ class DiscussionCommentModelTest(TestCase):
discussion_comment
.
description
=
'Discussion Comment after update'
mock_time
=
timezone
.
now
()
with
mock
.
patch
(
'django.utils.timezone.now'
,
mock
.
Mock
(
return_value
=
mock_time
)):
with
mock
.
patch
(
TIME_ZONE_MODULE
,
mock
.
Mock
(
return_value
=
mock_time
)):
discussion_comment
.
save
()
self
.
assertEqual
(
discussion_comment
.
updated_at
,
mock_time
)
self
.
assertNotEqual
(
discussion_comment
.
created_at
,
discussion_comment
.
updated_at
)
def
test_discussion_comment_has_many_to_many_relation_with_materi
(
self
):
discussion_comment
=
DiscussionComment
()
discussion_comment
.
description
=
self
.
DESCRIPTION_DEFAULT
discussion_comment
.
description
=
DESCRIPTION_DEFAULT
discussion_comment
.
user
=
self
.
USER_CONTRIBUTOR
discussion_comment
.
discussion
=
self
.
DISCUSSION
...
...
@@ -228,8 +236,7 @@ class ForumCreateDiscussionPageTest(TestCase):
def
test_create_discussion_page_success_submit_with_login
(
self
):
self
.
client
.
login
(
email
=
CONTRIBUTOR_EMAIL
,
password
=
CONTRIBUTOR_PASSWORD
)
title
=
'New Discussion'
description
=
'Discussion description'
request
=
self
.
client
.
post
(
URL_CREATE_DISCUSSION
,
{
'title'
:
title
,
'description'
:
description
})
request
=
self
.
client
.
post
(
URL_CREATE_DISCUSSION
,
{
'title'
:
title
,
'description'
:
DESCRIPTION_DEFAULT
})
self
.
assertEqual
(
request
.
status_code
,
302
)
new_discussion
=
Discussion
.
objects
.
get
(
title
=
title
)
...
...
@@ -238,10 +245,9 @@ class ForumCreateDiscussionPageTest(TestCase):
def
test_create_discussion_page_success_submit_multiple_materi
(
self
):
self
.
client
.
login
(
email
=
CONTRIBUTOR_EMAIL
,
password
=
CONTRIBUTOR_PASSWORD
)
title
=
'New Discussion'
description
=
'Discussion description'
materi_one
=
Materi
.
objects
.
create
(
uploader
=
self
.
USER_CONTRIBUTOR
,
status
=
"APPROVE"
,
title
=
"Materi 1"
)
materi_two
=
Materi
.
objects
.
create
(
uploader
=
self
.
USER_CONTRIBUTOR
,
status
=
"APPROVE"
,
title
=
"Materi 2"
)
request
=
self
.
client
.
post
(
URL_CREATE_DISCUSSION
,
{
'title'
:
title
,
'description'
:
description
,
request
=
self
.
client
.
post
(
URL_CREATE_DISCUSSION
,
{
'title'
:
title
,
'description'
:
DESCRIPTION_DEFAULT
,
'materi'
:
[
materi_one
.
id
,
materi_two
.
id
]})
self
.
assertEqual
(
request
.
status_code
,
302
)
new_discussion
=
Discussion
.
objects
.
get
(
title
=
title
)
...
...
@@ -292,13 +298,13 @@ class ForumDiscussionDetailPageTest(TestCase):
def
test_discussion_shows_correct_comment
(
self
):
discussion_id
=
str
(
self
.
DISCUSSION
.
id
)
DiscussionComment
.
objects
.
create
(
user
=
self
.
USER_CONTRIBUTOR
,
discussion
=
self
.
DISCUSSION
,
description
=
"Comment 1"
)
description
=
COMMENT_ONE
)
DiscussionComment
.
objects
.
create
(
user
=
self
.
USER_CONTRIBUTOR
,
discussion
=
self
.
DISCUSSION
,
description
=
"Comment 2"
)
description
=
COMMENT_TWO
)
request
=
self
.
client
.
get
(
URL_DISCUSSION_DETAIL
+
discussion_id
)
self
.
assertIn
(
"Comment 1"
,
request
.
content
.
decode
())
self
.
assertIn
(
"Comment 2"
,
request
.
content
.
decode
())
self
.
assertIn
(
COMMENT_ONE
,
request
.
content
.
decode
())
self
.
assertIn
(
COMMENT_TWO
,
request
.
content
.
decode
())
def
test_discussion_detail_page_show_delete_button_for_correct_user
(
self
):
self
.
client
.
login
(
email
=
CONTRIBUTOR_EMAIL
,
password
=
CONTRIBUTOR_PASSWORD
)
...
...
@@ -338,8 +344,8 @@ class ForumDiscussionDetailPageTest(TestCase):
self
.
client
.
login
(
email
=
CONTRIBUTOR_EMAIL
,
password
=
CONTRIBUTOR_PASSWORD
)
discussion_id
=
str
(
self
.
DISCUSSION
.
id
)
description
=
'DiscussionComment description'
materi_one
=
Materi
.
objects
.
create
(
uploader
=
self
.
USER_CONTRIBUTOR
,
status
=
"APPROVE"
,
title
=
"Materi 1"
)
materi_two
=
Materi
.
objects
.
create
(
uploader
=
self
.
USER_CONTRIBUTOR
,
status
=
"APPROVE"
,
title
=
"Materi 2"
)
materi_one
=
Materi
.
objects
.
create
(
uploader
=
self
.
USER_CONTRIBUTOR
,
status
=
"APPROVE"
,
title
=
MATERI_TITLE_ONE
)
materi_two
=
Materi
.
objects
.
create
(
uploader
=
self
.
USER_CONTRIBUTOR
,
status
=
"APPROVE"
,
title
=
MATERI_TITLE_TWO
)
request
=
self
.
client
.
post
(
URL_DISCUSSION_DETAIL
+
discussion_id
,
{
'description'
:
description
,
'materi'
:
[
materi_one
.
id
,
materi_two
.
id
]})
self
.
assertEqual
(
request
.
status_code
,
302
)
...
...
@@ -350,33 +356,33 @@ class ForumDiscussionDetailPageTest(TestCase):
def
test_discussion_delete_page_redirect_without_login
(
self
):
discussion_id
=
str
(
self
.
DISCUSSION
.
id
)
request
=
self
.
client
.
get
(
URL_DISCUSSION_DETAIL
+
discussion_id
+
'/delete'
)
request
=
self
.
client
.
get
(
URL_DISCUSSION_DETAIL
+
discussion_id
+
URL_DELETE
)
self
.
assertEqual
(
request
.
status_code
,
302
)
self
.
assertEqual
(
request
.
url
,
'/login?next=/forum/discussion/'
+
discussion_id
+
'/delete'
)
self
.
assertEqual
(
request
.
url
,
'/login?next=/forum/discussion/'
+
discussion_id
+
URL_DELETE
)
def
test_discussion_delete_page_exist
(
self
):
self
.
client
.
login
(
email
=
CONTRIBUTOR_EMAIL
,
password
=
CONTRIBUTOR_PASSWORD
)
discussion_id
=
str
(
self
.
DISCUSSION
.
id
)
request
=
self
.
client
.
get
(
URL_DISCUSSION_DETAIL
+
discussion_id
+
'/delete'
)
request
=
self
.
client
.
get
(
URL_DISCUSSION_DETAIL
+
discussion_id
+
URL_DELETE
)
self
.
assertEqual
(
request
.
status_code
,
200
)
def
test_discussion_delete_page_using_correct_template
(
self
):
self
.
client
.
login
(
email
=
CONTRIBUTOR_EMAIL
,
password
=
CONTRIBUTOR_PASSWORD
)
discussion_id
=
str
(
self
.
DISCUSSION
.
id
)
request
=
self
.
client
.
get
(
URL_DISCUSSION_DETAIL
+
discussion_id
+
'/delete'
)
request
=
self
.
client
.
get
(
URL_DISCUSSION_DETAIL
+
discussion_id
+
URL_DELETE
)
self
.
assertTemplateUsed
(
request
,
'forum/forum_discussion_delete.html'
)
def
test_discussion_delete_without_correct_credential_cannot_delete
(
self
):
User
.
objects
.
create_contributor
(
email
=
'test@mail.com'
,
password
=
'test'
)
self
.
client
.
login
(
email
=
'test@mail.com'
,
password
=
'test'
)
User
.
objects
.
create_contributor
(
email
=
TEST_EMAIL
,
password
=
CONTRIBUTOR_PASSWORD
)
self
.
client
.
login
(
email
=
TEST_EMAIL
,
password
=
CONTRIBUTOR_PASSWORD
)
discussion_id
=
str
(
self
.
DISCUSSION
.
id
)
request
=
self
.
client
.
get
(
URL_DISCUSSION_DETAIL
+
discussion_id
+
'/delete'
)
request
=
self
.
client
.
get
(
URL_DISCUSSION_DETAIL
+
discussion_id
+
URL_DELETE
)
self
.
assertIn
(
'You are not authorized to delete this discussion'
,
request
.
content
.
decode
())
request
=
self
.
client
.
post
(
URL_DISCUSSION_DETAIL
+
discussion_id
+
'/delete'
)
request
=
self
.
client
.
post
(
URL_DISCUSSION_DETAIL
+
discussion_id
+
URL_DELETE
)
self
.
assertEqual
(
request
.
status_code
,
302
)
self
.
assertEqual
(
Discussion
.
objects
.
get
(
id
=
discussion_id
),
self
.
DISCUSSION
)
...
...
@@ -384,10 +390,10 @@ class ForumDiscussionDetailPageTest(TestCase):
self
.
client
.
login
(
email
=
CONTRIBUTOR_EMAIL
,
password
=
CONTRIBUTOR_PASSWORD
)
discussion_id
=
str
(
self
.
DISCUSSION
.
id
)
request
=
self
.
client
.
get
(
URL_DISCUSSION_DETAIL
+
discussion_id
+
'/delete'
)
request
=
self
.
client
.
get
(
URL_DISCUSSION_DETAIL
+
discussion_id
+
URL_DELETE
)
self
.
assertIn
(
'Are you sure you want to delete'
,
request
.
content
.
decode
())
request
=
self
.
client
.
post
(
URL_DISCUSSION_DETAIL
+
discussion_id
+
'/delete'
)
request
=
self
.
client
.
post
(
URL_DISCUSSION_DETAIL
+
discussion_id
+
URL_DELETE
)
self
.
assertEqual
(
request
.
status_code
,
302
)
with
self
.
assertRaises
(
ObjectDoesNotExist
):
Discussion
.
objects
.
get
(
id
=
discussion_id
)
...
...
@@ -395,10 +401,11 @@ class ForumDiscussionDetailPageTest(TestCase):
def
test_discussion_comment_delete_page_redirect_without_login
(
self
):
discussion_id
=
str
(
self
.
DISCUSSION
.
id
)
comment
=
DiscussionComment
.
objects
.
create
(
discussion
=
self
.
DISCUSSION
,
user
=
self
.
USER_CONTRIBUTOR
,
description
=
"Comment 1"
)
description
=
COMMENT_ONE
)
comment_id
=
str
(
comment
.
id
)
request
=
self
.
client
.
get
(
'{0}{1}/{2}/delete'
.
format
(
URL_DISCUSSION_DETAIL
,
discussion_id
,
comment_id
))
request
=
self
.
client
.
get
(
URL_DELETE_DISCUSSION_COMMENT
.
format
(
URL_DISCUSSION_DETAIL
,
discussion_id
,
comment_id
))
self
.
assertEqual
(
request
.
status_code
,
302
)
self
.
assertEqual
(
request
.
url
,
'/login?next=/forum/discussion/{0}/{1}/delete'
.
format
(
discussion_id
,
comment_id
))
...
...
@@ -406,34 +413,38 @@ class ForumDiscussionDetailPageTest(TestCase):
self
.
client
.
login
(
email
=
CONTRIBUTOR_EMAIL
,
password
=
CONTRIBUTOR_PASSWORD
)
discussion_id
=
str
(
self
.
DISCUSSION
.
id
)
comment
=
DiscussionComment
.
objects
.
create
(
discussion
=
self
.
DISCUSSION
,
user
=
self
.
USER_CONTRIBUTOR
,
description
=
"Comment 1"
)
description
=
COMMENT_ONE
)
comment_id
=
str
(
comment
.
id
)
request
=
self
.
client
.
get
(
'{0}{1}/{2}/delete'
.
format
(
URL_DISCUSSION_DETAIL
,
discussion_id
,
comment_id
))
request
=
self
.
client
.
get
(
URL_DELETE_DISCUSSION_COMMENT
.
format
(
URL_DISCUSSION_DETAIL
,
discussion_id
,
comment_id
))
self
.
assertEqual
(
request
.
status_code
,
200
)
def
test_discussion_comment_delete_page_using_correct_template
(
self
):
self
.
client
.
login
(
email
=
CONTRIBUTOR_EMAIL
,
password
=
CONTRIBUTOR_PASSWORD
)
discussion_id
=
str
(
self
.
DISCUSSION
.
id
)
comment
=
DiscussionComment
.
objects
.
create
(
discussion
=
self
.
DISCUSSION
,
user
=
self
.
USER_CONTRIBUTOR
,
description
=
"Comment 1"
)
description
=
COMMENT_ONE
)
comment_id
=
str
(
comment
.
id
)
request
=
self
.
client
.
get
(
'{0}{1}/{2}/delete'
.
format
(
URL_DISCUSSION_DETAIL
,
discussion_id
,
comment_id
))
request
=
self
.
client
.
get
(
URL_DELETE_DISCUSSION_COMMENT
.
format
(
URL_DISCUSSION_DETAIL
,
discussion_id
,
comment_id
))
self
.
assertTemplateUsed
(
request
,
'forum/forum_discussion_delete.html'
)
def
test_discussion_comment_delete_without_correct_credential_cannot_delete
(
self
):
User
.
objects
.
create_contributor
(
email
=
'test@mail.com'
,
password
=
'test'
)
self
.
client
.
login
(
email
=
'test@mail.com'
,
password
=
'test'
)
User
.
objects
.
create_contributor
(
email
=
TEST_EMAIL
,
password
=
CONTRIBUTOR_PASSWORD
)
self
.
client
.
login
(
email
=
TEST_EMAIL
,
password
=
CONTRIBUTOR_PASSWORD
)
discussion_id
=
str
(
self
.
DISCUSSION
.
id
)
comment
=
DiscussionComment
.
objects
.
create
(
discussion
=
self
.
DISCUSSION
,
user
=
self
.
USER_CONTRIBUTOR
,
description
=
"Comment 1"
)
description
=
COMMENT_ONE
)
comment_id
=
str
(
comment
.
id
)
request
=
self
.
client
.
get
(
'{0}{1}/{2}/delete'
.
format
(
URL_DISCUSSION_DETAIL
,
discussion_id
,
comment_id
))
request
=
self
.
client
.
get
(
URL_DELETE_DISCUSSION_COMMENT
.
format
(
URL_DISCUSSION_DETAIL
,
discussion_id
,
comment_id
))
self
.
assertIn
(
'You are not authorized to delete this discussion'
,
request
.
content
.
decode
())
request
=
self
.
client
.
post
(
'{0}{1}/{2}/delete'
.
format
(
URL_DISCUSSION_DETAIL
,
discussion_id
,
comment_id
))
request
=
self
.
client
.
post
(
URL_DELETE_DISCUSSION_COMMENT
.
format
(
URL_DISCUSSION_DETAIL
,
discussion_id
,
comment_id
))
self
.
assertEqual
(
request
.
status_code
,
302
)
self
.
assertEqual
(
DiscussionComment
.
objects
.
get
(
id
=
comment_id
),
comment
)
...
...
@@ -441,13 +452,15 @@ class ForumDiscussionDetailPageTest(TestCase):
self
.
client
.
login
(
email
=
CONTRIBUTOR_EMAIL
,
password
=
CONTRIBUTOR_PASSWORD
)
discussion_id
=
str
(
self
.
DISCUSSION
.
id
)
comment
=
DiscussionComment
.
objects
.
create
(
discussion
=
self
.
DISCUSSION
,
user
=
self
.
USER_CONTRIBUTOR
,
description
=
"Comment 1"
)
description
=
COMMENT_ONE
)
comment_id
=
str
(
comment
.
id
)
request
=
self
.
client
.
get
(
'{0}{1}/{2}/delete'
.
format
(
URL_DISCUSSION_DETAIL
,
discussion_id
,
comment_id
))
request
=
self
.
client
.
get
(
URL_DELETE_DISCUSSION_COMMENT
.
format
(
URL_DISCUSSION_DETAIL
,
discussion_id
,
comment_id
))
self
.
assertIn
(
'Are you sure you want to delete'
,
request
.
content
.
decode
())
request
=
self
.
client
.
post
(
'{0}{1}/{2}/delete'
.
format
(
URL_DISCUSSION_DETAIL
,
discussion_id
,
comment_id
))
request
=
self
.
client
.
post
(
URL_DELETE_DISCUSSION_COMMENT
.
format
(
URL_DISCUSSION_DETAIL
,
discussion_id
,
comment_id
))
self
.
assertEqual
(
request
.
status_code
,
302
)
with
self
.
assertRaises
(
ObjectDoesNotExist
):
DiscussionComment
.
objects
.
get
(
id
=
comment_id
)
...
...
@@ -456,14 +469,14 @@ class ForumDiscussionDetailPageTest(TestCase):
class
DiscussionFormTest
(
TestCase
):
def
setUp
(
self
):
self
.
USER_CONTRIBUTOR
=
User
.
objects
.
create_contributor
(
email
=
CONTRIBUTOR_EMAIL
,
password
=
CONTRIBUTOR_PASSWORD
)
self
.
MATERI
=
Materi
.
objects
.
create
(
uploader
=
self
.
USER_CONTRIBUTOR
,
status
=
"APPROVE"
,
title
=
"Materi 1"
)
self
.
MATERI
=
Materi
.
objects
.
create
(
uploader
=
self
.
USER_CONTRIBUTOR
,
status
=
"APPROVE"
,
title
=
MATERI_TITLE_ONE
)
def
test_discussion_form_show_correct_fields
(
self
):
form
=
DiscussionForm
()
self
.
assertIn
(
"Title"
,
form
.
__str__
())
self
.
assertIn
(
"Description"
,
form
.
__str__
())
self
.
assertIn
(
"Materi"
,
form
.
__str__
())
self
.
assertIn
(
"Materi 1"
,
form
.
__str__
())
self
.
assertIn
(
MATERI_TITLE_ONE
,
form
.
__str__
())
def
test_create_discussion_form_submit_without_title_fail
(
self
):
form
=
DiscussionForm
({
'description'
:
'Create discussion without title'
})
...
...
@@ -476,14 +489,14 @@ class DiscussionFormTest(TestCase):
form
.
save
()
def
test_create_discussion_form_submit_success
(
self
):
form
=
DiscussionForm
({
'title'
:
'Success'
,
'description'
:
'Complete valid data'
})
form
=
DiscussionForm
({
'title'
:
'Success'
,
'description'
:
COMPLETE_VALID_DATA
})
self
.
assertTrue
(
form
.
is_valid
())
form
.
instance
.
user
=
self
.
USER_CONTRIBUTOR
discussion
=
form
.
save
()
self
.
assertEqual
(
discussion
.
user
,
self
.
USER_CONTRIBUTOR
)
self
.
assertEqual
(
discussion
.
title
,
'Success'
)
self
.
assertEqual
(
discussion
.
description
,
'Complete valid data'
)
self
.
assertEqual
(
discussion
.
description
,
COMPLETE_VALID_DATA
)
class
DiscussionCommentFormTest
(
TestCase
):
...
...
@@ -492,13 +505,13 @@ class DiscussionCommentFormTest(TestCase):
self
.
DISCUSSION
=
Discussion
.
objects
.
create
(
title
=
'Discussion Sample'
,
description
=
'Discussion Sample Description'
,
user
=
self
.
USER_CONTRIBUTOR
)
self
.
MATERI
=
Materi
.
objects
.
create
(
uploader
=
self
.
USER_CONTRIBUTOR
,
status
=
"APPROVE"
,
title
=
"Materi 1"
)
self
.
MATERI
=
Materi
.
objects
.
create
(
uploader
=
self
.
USER_CONTRIBUTOR
,
status
=
"APPROVE"
,
title
=
MATERI_TITLE_ONE
)
def
test_discussion_comment_form_show_correct_fields
(
self
):
form
=
DiscussionCommentForm
()
self
.
assertIn
(
"Description"
,
form
.
__str__
())
self
.
assertIn
(
"Materi"
,
form
.
__str__
())
self
.
assertIn
(
"Materi 1"
,
form
.
__str__
())
self
.
assertIn
(
MATERI_TITLE_ONE
,
form
.
__str__
())
def
test_create_discussion_comment_form_submit_without_discussion_fail
(
self
):
form
=
DiscussionCommentForm
({
'description'
:
'Create discussion without title'
})
...
...
@@ -513,11 +526,11 @@ class DiscussionCommentFormTest(TestCase):
form
.
save
()
def
test_create_discussion_comment_form_submit_success
(
self
):
form
=
DiscussionCommentForm
({
'description'
:
'Complete valid data'
})
form
=
DiscussionCommentForm
({
'description'
:
COMPLETE_VALID_DATA
})
self
.
assertTrue
(
form
.
is_valid
())
form
.
instance
.
user
=
self
.
USER_CONTRIBUTOR
form
.
instance
.
discussion
=
self
.
DISCUSSION
discussion_comment
=
form
.
save
()
self
.
assertEqual
(
discussion_comment
.
user
,
self
.
USER_CONTRIBUTOR
)
self
.
assertEqual
(
discussion_comment
.
description
,
'Complete valid data'
)
self
.
assertEqual
(
discussion_comment
.
description
,
COMPLETE_VALID_DATA
)
forum/views.py
View file @
5a5a3ba9
...
...
@@ -9,6 +9,9 @@ from django.views.generic.list import MultipleObjectMixin
from
forum.forms
import
DiscussionForm
,
DiscussionCommentForm
from
forum.models
import
Discussion
,
DiscussionComment
URL_FORUM_HOME_PAGE
=
'/forum'
URL_LOGIN_PAGE
=
'/login'
class
ForumHomePage
(
ListView
):
paginate_by
=
10
...
...
@@ -18,9 +21,9 @@ class ForumHomePage(ListView):
class
ForumCreateDiscussion
(
LoginRequiredMixin
,
CreateView
):
form_class
=
DiscussionForm
success_url
=
'/forum'
success_url
=
URL_FORUM_HOME_PAGE
template_name
=
'forum/forum_discussion_create.html'
login_url
=
'/login'
login_url
=
URL_LOGIN_PAGE
def
form_valid
(
self
,
form
):
form
.
instance
.
user
=
self
.
request
.
user
...
...
@@ -35,20 +38,20 @@ class ForumDeleteDiscussion(LoginRequiredMixin, DeleteView):
model
=
Discussion
success_url
=
reverse_lazy
(
'forum_home'
)
template_name
=
'forum/forum_discussion_delete.html'
login_url
=
'/login'
login_url
=
URL_LOGIN_PAGE
def
delete
(
self
,
request
,
*
args
,
**
kwargs
):
discussion
=
self
.
get_object
()
if
discussion
.
user
==
request
.
user
:
discussion
.
delete
()
return
redirect
(
'/forum'
)
return
redirect
(
URL_FORUM_HOME_PAGE
)
class
ForumDiscussionDetail
(
FormMixin
,
DetailView
,
MultipleObjectMixin
):
model
=
Discussion
template_name
=
'forum/forum_discussion_detail.html'
form_class
=
DiscussionCommentForm
success_url
=
'/forum'
success_url
=
URL_FORUM_HOME_PAGE
paginate_by
=
20
def
get_success_url
(
self
):
...
...
@@ -82,7 +85,7 @@ class ForumDiscussionDetail(FormMixin, DetailView, MultipleObjectMixin):
class
ForumDiscussionCommentDelete
(
LoginRequiredMixin
,
DeleteView
):
model
=
DiscussionComment
template_name
=
'forum/forum_discussion_delete.html'
login_url
=
'/login'
login_url
=
URL_LOGIN_PAGE
def
delete
(
self
,
request
,
*
args
,
**
kwargs
):
discussion_comment
=
self
.
get_object
()
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment