-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from Ajinx1/ayotee-dev
fixed all tests and status_code
- Loading branch information
Showing
5 changed files
with
28 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ package tests | |
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
@@ -12,6 +13,7 @@ import ( | |
"github.com/hngprojects/hng_boilerplate_golang_web/internal/models" | ||
"github.com/hngprojects/hng_boilerplate_golang_web/pkg/controller/newsletter" | ||
"github.com/hngprojects/hng_boilerplate_golang_web/pkg/repository/storage" | ||
"github.com/hngprojects/hng_boilerplate_golang_web/utility" | ||
) | ||
|
||
func setupNewsLetterTestRouter() (*gin.Engine, *newsletter.Controller) { | ||
|
@@ -40,8 +42,9 @@ func TestE2ENewsletterSubscription(t *testing.T) { | |
router, _ := setupNewsLetterTestRouter() | ||
|
||
// Test POST /newsletter | ||
currUUID := utility.GenerateUUID() | ||
body := models.NewsLetter{ | ||
Email: "[email protected]", | ||
Email: fmt.Sprintf("testuser%[email protected]", currUUID), | ||
} | ||
jsonBody, err := json.Marshal(body) | ||
if err != nil { | ||
|
@@ -66,8 +69,9 @@ func TestE2ENewsletterSubscription(t *testing.T) { | |
func TestPostNewsletter_ValidateEmail(t *testing.T) { | ||
router, _ := setupNewsLetterTestRouter() | ||
|
||
currUUID := utility.GenerateUUID() | ||
body := models.NewsLetter{ | ||
Email: "invalid-email", | ||
Email: fmt.Sprintf("testuser%v@qa", currUUID), | ||
} | ||
jsonBody, _ := json.Marshal(body) | ||
|
||
|
@@ -85,11 +89,13 @@ func TestPostNewsletter_ValidateEmail(t *testing.T) { | |
func TestPostNewsletter_CheckDuplicateEmail(t *testing.T) { | ||
router, newsController := setupNewsLetterTestRouter() | ||
|
||
currUUID := utility.GenerateUUID() | ||
|
||
db := newsController.Db.Postgresql | ||
db.Create(&models.NewsLetter{Email: "[email protected]"}) | ||
db.Create(&models.NewsLetter{Email: fmt.Sprintf("testuser%[email protected]", currUUID)}) | ||
|
||
body := models.NewsLetter{ | ||
Email: "[email protected]", | ||
Email: fmt.Sprintf("testuser%[email protected]", currUUID), | ||
} | ||
jsonBody, _ := json.Marshal(body) | ||
|
||
|
@@ -107,8 +113,9 @@ func TestPostNewsletter_CheckDuplicateEmail(t *testing.T) { | |
func TestPostNewsletter_SaveData(t *testing.T) { | ||
router, newsController := setupNewsLetterTestRouter() | ||
|
||
currUUID := utility.GenerateUUID() | ||
body := models.NewsLetter{ | ||
Email: "[email protected]", | ||
Email: fmt.Sprintf("testuser%[email protected]", currUUID), | ||
} | ||
jsonBody, _ := json.Marshal(body) | ||
|
||
|
@@ -123,17 +130,18 @@ func TestPostNewsletter_SaveData(t *testing.T) { | |
AssertResponseMessage(t, response["message"].(string), "subscribed successfully") | ||
|
||
var newsletter models.NewsLetter | ||
newsController.Db.Postgresql.First(&newsletter, "email = ?", "[email protected]") | ||
if newsletter.Email != "[email protected]" { | ||
t.Errorf("data not saved correctly to the database: expected email %s, got %s", "[email protected]", newsletter.Email) | ||
newsController.Db.Postgresql.First(&newsletter, "email = ?", fmt.Sprintf("testuser%[email protected]", currUUID)) | ||
if newsletter.Email != fmt.Sprintf("testuser%[email protected]", currUUID) { | ||
t.Errorf("data not saved correctly to the database: expected email %s, got %s", fmt.Sprintf("testuser%[email protected]", currUUID), newsletter.Email) | ||
} | ||
} | ||
|
||
func TestPostNewsletter_ResponseAndStatusCode(t *testing.T) { | ||
router, _ := setupNewsLetterTestRouter() | ||
|
||
currUUID := utility.GenerateUUID() | ||
body := models.NewsLetter{ | ||
Email: "test3@example.com", | ||
Email: fmt.Sprintf("testuser%v@gmail.com", currUUID), | ||
} | ||
jsonBody, _ := json.Marshal(body) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters