Testing the 'Create Article' Functionality
The tests for this section of code should check that the articles are added to the existing list of articles.
To test the createNewArticle
function in models.article.go
, update themodels.article_test.go
with the following test:
// models.article_test.go
func TestCreateNewArticle(t *testing.T) {
originalLength := len(getAllArticles())
a, err := createNewArticle("New test title", "New test content")
allArticles := getAllArticles()
newLength := len(allArticles)
if err != nil || newLength != originalLength+1 ||
a.Title != "New test title" || a.Content != "New test content" {
t.Fail()
}
}
This test uses the createNewArticle
function to add a new article and checks that the article list contains the new article.
The test for the handler that processes the article creation request will be similar to the test that tested the registration functionality.
The handlers_article_test.go
file should be updated to include the following test:
// handlers.article_test.go
func TestArticleCreationAuthenticated(t *testing.T) {
saveLists()
w := httptest.NewRecorder()
r := getRouter(true)
http.SetCookie(w, &http.Cookie{Name: "token", Value: "123"})
r.POST("/article/create", createArticle)
articlePayload := getArticlePOSTPayload()
req, _ := http.NewRequest("POST", "/article/create", strings.NewReader(articlePayload))
req.Header = http.Header{"Cookie": w.HeaderMap["Set-Cookie"]}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Content-Length", strconv.Itoa(len(articlePayload)))
r.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fail()
}
p, err := ioutil.ReadAll(w.Body)
if err != nil || strings.Index(string(p), "<title>Submission Successful</title>") < 0 {
t.Fail()
}
restoreLists()
}
func getArticlePOSTPayload() string {
params := url.Values{}
params.Add("title", "Test Article Title")
params.Add("content", "Test Article Content")
return params.Encode()
}
Run these tests using the following command:
go test -v
The tests should fail and you should see something similar to the following
=== RUN TestShowIndexPageUnauthenticated
[GIN] 2016/09/04 - 10:04:52 | 200 | 211.568µs | | GET /
--- PASS: TestShowIndexPageUnauthenticated (0.00s)
=== RUN TestArticleUnauthenticated
[GIN] 2016/09/04 - 10:04:52 | 200 | 102.277µs | | GET /article/view/1
--- PASS: TestArticleUnauthenticated (0.00s)
=== RUN TestArticleListJSON
[GIN] 2016/09/04 - 10:04:52 | 200 | 35.975µs | | GET /
--- PASS: TestArticleListJSON (0.00s)
=== RUN TestArticleXML
[GIN] 2016/09/04 - 10:04:52 | 200 | 25.814µs | | GET /article/view/1
--- PASS: TestArticleXML (0.00s)
=== RUN TestArticleCreationAuthenticated
[GIN] 2016/09/04 - 10:04:52 | 200 | 426ns | | POST /article/create
--- FAIL: TestArticleCreationAuthenticated (0.00s)
=== RUN TestShowRegistrationPageUnauthenticated
[GIN] 2016/09/04 - 10:04:52 | 200 | 112.875µs | | GET /u/register
--- PASS: TestShowRegistrationPageUnauthenticated (0.00s)
=== RUN TestRegisterUnauthenticated
[GIN] 2016/09/04 - 10:04:52 | 200 | 132.996µs | | POST /u/register
--- PASS: TestRegisterUnauthenticated (0.00s)
=== RUN TestRegisterUnauthenticatedUnavailableUsername
[GIN] 2016/09/04 - 10:04:52 | 400 | 116.116µs | | POST /u/register
--- PASS: TestRegisterUnauthenticatedUnavailableUsername (0.00s)
=== RUN TestShowLoginPageUnauthenticated
[GIN] 2016/09/04 - 10:04:52 | 200 | 92.27µs | | GET /u/login
--- PASS: TestShowLoginPageUnauthenticated (0.00s)
=== RUN TestLoginUnauthenticated
[GIN] 2016/09/04 - 10:04:52 | 200 | 97.427µs | | POST /u/login
--- PASS: TestLoginUnauthenticated (0.00s)
=== RUN TestLoginUnauthenticatedIncorrectCredentials
[GIN] 2016/09/04 - 10:04:52 | 400 | 107.117µs | | POST /u/login
--- PASS: TestLoginUnauthenticatedIncorrectCredentials (0.00s)
=== RUN TestGetAllArticles
--- PASS: TestGetAllArticles (0.00s)
=== RUN TestGetArticleByID
--- PASS: TestGetArticleByID (0.00s)
=== RUN TestCreateNewArticle
--- FAIL: TestCreateNewArticle (0.00s)
=== RUN TestValidUserRegistration
--- PASS: TestValidUserRegistration (0.00s)
=== RUN TestInvalidUserRegistration
--- PASS: TestInvalidUserRegistration (0.00s)
=== RUN TestUsernameAvailability
--- PASS: TestUsernameAvailability (0.00s)
=== RUN TestUserValidity
--- PASS: TestUserValidity (0.00s)
FAIL
exit status 1
FAIL github.com/demo-apps/go-gin-app 0.009s
With the tests in place, let's start implementing the functionality to allow article submission.