mirror of
https://github.com/pushbits/server.git
synced 2025-05-10 23:47:00 +02:00
use require to stop failing tests
This commit is contained in:
parent
a6fa829165
commit
125e9ddd53
5 changed files with 42 additions and 43 deletions
|
@ -16,6 +16,7 @@ import (
|
|||
"github.com/pushbits/server/tests"
|
||||
"github.com/pushbits/server/tests/mockups"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
var TestApplicationHandler *ApplicationHandler
|
||||
|
@ -99,6 +100,7 @@ func TestApi_RegisterApplicationWithoutUser(t *testing.T) {
|
|||
|
||||
func TestApi_RegisterApplication(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
require := require.New(t)
|
||||
gin.SetMode(gin.TestMode)
|
||||
|
||||
testCases := make([]tests.Request, 0)
|
||||
|
@ -121,15 +123,9 @@ func TestApi_RegisterApplication(t *testing.T) {
|
|||
// Parse body only for successful requests
|
||||
if req.ShouldStatus >= 200 && req.ShouldStatus < 300 {
|
||||
body, err := ioutil.ReadAll(w.Body)
|
||||
assert.NoErrorf(err, "Can not read request body")
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
require.NoErrorf(err, "Can not read request body")
|
||||
err = json.Unmarshal(body, &application)
|
||||
assert.NoErrorf(err, "Can not unmarshal request body")
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
require.NoErrorf(err, "Can not unmarshal request body")
|
||||
|
||||
SuccessAplications[user.ID] = append(SuccessAplications[user.ID], application)
|
||||
}
|
||||
|
@ -143,6 +139,7 @@ func TestApi_GetApplications(t *testing.T) {
|
|||
var applications []model.Application
|
||||
|
||||
assert := assert.New(t)
|
||||
require := require.New(t)
|
||||
gin.SetMode(gin.TestMode)
|
||||
|
||||
testCases := make([]tests.Request, 0)
|
||||
|
@ -161,12 +158,9 @@ func TestApi_GetApplications(t *testing.T) {
|
|||
// Parse body only for successful requests
|
||||
if req.ShouldStatus >= 200 && req.ShouldStatus < 300 {
|
||||
body, err := ioutil.ReadAll(w.Body)
|
||||
assert.NoErrorf(err, "Can not read request body")
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
require.NoErrorf(err, "Can not read request body")
|
||||
err = json.Unmarshal(body, &applications)
|
||||
assert.NoErrorf(err, "Can not unmarshal request body")
|
||||
require.NoErrorf(err, "Can not unmarshal request body")
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
@ -225,6 +219,7 @@ func TestApi_GetApplication(t *testing.T) {
|
|||
var application model.Application
|
||||
|
||||
assert := assert.New(t)
|
||||
require := require.New(t)
|
||||
gin.SetMode(gin.TestMode)
|
||||
|
||||
// Previously generated applications
|
||||
|
@ -244,15 +239,9 @@ func TestApi_GetApplication(t *testing.T) {
|
|||
// Parse body only for successful requests
|
||||
if req.ShouldStatus >= 200 && req.ShouldStatus < 300 {
|
||||
body, err := ioutil.ReadAll(w.Body)
|
||||
assert.NoErrorf(err, "Can not read request body")
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
require.NoErrorf(err, "Can not read request body")
|
||||
err = json.Unmarshal(body, &application)
|
||||
assert.NoErrorf(err, "Can not unmarshal request body: %v", err)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
require.NoErrorf(err, "Can not unmarshal request body: %v", err)
|
||||
|
||||
assert.Equalf(application.ID, app.ID, "Application ID should be %d but is %d", app.ID, application.ID)
|
||||
assert.Equalf(application.Name, app.Name, "Application Name should be %s but is %s", app.Name, application.Name)
|
||||
|
@ -267,6 +256,7 @@ func TestApi_GetApplication(t *testing.T) {
|
|||
|
||||
func TestApi_UpdateApplication(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
require := require.New(t)
|
||||
gin.SetMode(gin.TestMode)
|
||||
|
||||
for _, user := range TestUsers {
|
||||
|
@ -278,7 +268,7 @@ func TestApi_UpdateApplication(t *testing.T) {
|
|||
Name: &newName,
|
||||
}
|
||||
updateAppBytes, err := json.Marshal(updateApp)
|
||||
assert.NoErrorf(err, "Error on marshaling updateApplication struct")
|
||||
require.NoErrorf(err, "Error on marshaling updateApplication struct")
|
||||
|
||||
// Valid
|
||||
testCases[app.ID] = tests.Request{Name: fmt.Sprintf("Update application (valid) %s (%d)", app.Name, app.ID), Method: "PUT", Endpoint: fmt.Sprintf("/application/%d", app.ID), ShouldStatus: 200, Data: string(updateAppBytes), Headers: map[string]string{"Content-Type": "application/json"}}
|
||||
|
|
|
@ -8,10 +8,12 @@ import (
|
|||
"github.com/pushbits/server/tests"
|
||||
"github.com/pushbits/server/tests/mockups"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestApi_getID(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
require := require.New(t)
|
||||
gin.SetMode(gin.TestMode)
|
||||
testValue := uint(1337)
|
||||
|
||||
|
@ -36,11 +38,12 @@ func TestApi_getID(t *testing.T) {
|
|||
idReturned, err := getID(c)
|
||||
|
||||
if req.ShouldStatus >= 200 && req.ShouldStatus < 300 {
|
||||
require.NoErrorf(err, "getId with id %v (%t) returned an error altough it should not: %v", id, id, err)
|
||||
|
||||
idUint, ok := id.(uint)
|
||||
if ok {
|
||||
assert.Equalf(idReturned, idUint, "getApi id was set to %d but result is %d", idUint, idReturned)
|
||||
}
|
||||
assert.NoErrorf(err, "getId with id %v (%t) returned an error altough it should not: %v", id, id, err)
|
||||
} else {
|
||||
assert.Errorf(err, "getId with id %v (%t) returned no error altough it should", id, id)
|
||||
}
|
||||
|
@ -51,6 +54,7 @@ func TestApi_getID(t *testing.T) {
|
|||
|
||||
func TestApi_getApplication(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
require := require.New(t)
|
||||
gin.SetMode(gin.TestMode)
|
||||
|
||||
applications := mockups.GetAllApplications()
|
||||
|
@ -72,8 +76,8 @@ func TestApi_getApplication(t *testing.T) {
|
|||
app, err := getApplication(c, TestDatabase)
|
||||
|
||||
if req.ShouldStatus >= 200 && req.ShouldStatus < 300 {
|
||||
require.NoErrorf(err, "getApplication with id %v (%t) returned an error altough it should not: %v", id, id, err)
|
||||
assert.Equalf(app.ID, id, "getApplication id was set to %d but resulting app id is %d", id, app.ID)
|
||||
assert.NoErrorf(err, "getApplication with id %v (%t) returned an error altough it should not: %v", id, id, err)
|
||||
} else {
|
||||
assert.Errorf(err, "getApplication with id %v (%t) returned no error altough it should", id, id)
|
||||
}
|
||||
|
@ -85,6 +89,7 @@ func TestApi_getApplication(t *testing.T) {
|
|||
|
||||
func TestApi_getUser(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
require := require.New(t)
|
||||
gin.SetMode(gin.TestMode)
|
||||
|
||||
_, err := mockups.AddUsersToDb(TestDatabase, TestUsers)
|
||||
|
@ -106,8 +111,9 @@ func TestApi_getUser(t *testing.T) {
|
|||
user, err := getUser(c, TestDatabase)
|
||||
|
||||
if req.ShouldStatus >= 200 && req.ShouldStatus < 300 {
|
||||
require.NoErrorf(err, "getUser with id %v (%t) returned an error altough it should not: %v", id, id, err)
|
||||
assert.Equalf(user.ID, id, "getUser id was set to %d but resulting app id is %d", id, user.ID)
|
||||
assert.NoErrorf(err, "getUser with id %v (%t) returned an error altough it should not: %v", id, id, err)
|
||||
|
||||
} else {
|
||||
assert.Errorf(err, "getUser with id %v (%t) returned no error altough it should", id, id)
|
||||
}
|
||||
|
|
|
@ -9,10 +9,12 @@ import (
|
|||
"github.com/pushbits/server/internal/model"
|
||||
"github.com/pushbits/server/tests"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestApi_CreateNotification(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
require := require.New(t)
|
||||
gin.SetMode(gin.TestMode)
|
||||
|
||||
testApplication := model.Application{
|
||||
|
@ -43,15 +45,9 @@ func TestApi_CreateNotification(t *testing.T) {
|
|||
// Parse body only for successful requests
|
||||
if req.ShouldStatus >= 200 && req.ShouldStatus < 300 {
|
||||
body, err := ioutil.ReadAll(w.Body)
|
||||
assert.NoErrorf(err, "Can not read request body")
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
require.NoErrorf(err, "Can not read request body")
|
||||
err = json.Unmarshal(body, ¬ification)
|
||||
assert.NoErrorf(err, "Can not unmarshal request body")
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
require.NoErrorf(err, "Can not unmarshal request body")
|
||||
|
||||
shouldNotification, ok := req.ShouldReturn.(model.Notification)
|
||||
assert.Truef(ok, "(Test case %s) Type mismatch can not test further", req.Name)
|
||||
|
@ -83,7 +79,7 @@ func TestApi_DeleteNotification(t *testing.T) {
|
|||
testCases := make(map[interface{}]tests.Request)
|
||||
testCases["1"] = tests.Request{Name: "Valid numeric string", Method: "DELETE", Endpoint: "/message?token=123456&message=testmessage", ShouldStatus: 200}
|
||||
testCases["abcde"] = tests.Request{Name: "Valid string", Method: "DELETE", Endpoint: "/message?token=123456&message=testmessage", ShouldStatus: 200}
|
||||
testCases[123456] = tests.Request{Name: "Valid int", Method: "DELETE", Endpoint: "/message?token=123456&message=testmessage", ShouldStatus: 200}
|
||||
testCases[123456] = tests.Request{Name: "Invalid int", Method: "DELETE", Endpoint: "/message?token=123456&message=testmessage", ShouldStatus: 500}
|
||||
|
||||
for id, req := range testCases {
|
||||
w, c, err := req.GetRequest()
|
||||
|
@ -93,7 +89,7 @@ func TestApi_DeleteNotification(t *testing.T) {
|
|||
|
||||
c.Set("app", &testApplication)
|
||||
c.Set("messageid", id)
|
||||
TestNotificationHandler.CreateNotification(c)
|
||||
TestNotificationHandler.DeleteNotification(c)
|
||||
|
||||
assert.Equalf(w.Code, req.ShouldStatus, "(Test case: \"%s\") should return status code %v but is %v.", req.Name, req.ShouldStatus, w.Code)
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@ import (
|
|||
"github.com/pushbits/server/internal/model"
|
||||
"github.com/pushbits/server/tests"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestApi_CreateUser(t *testing.T) {
|
||||
|
@ -55,6 +56,7 @@ func TestApi_CreateUser(t *testing.T) {
|
|||
|
||||
func TestApi_GetUsers(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
require := require.New(t)
|
||||
|
||||
request := tests.Request{
|
||||
Method: "GET",
|
||||
|
@ -72,9 +74,9 @@ func TestApi_GetUsers(t *testing.T) {
|
|||
// Get users from body
|
||||
users := make([]model.ExternalUser, 0)
|
||||
usersRaw, err := ioutil.ReadAll(w.Body)
|
||||
assert.NoErrorf(err, "Failed to parse response body")
|
||||
require.NoErrorf(err, "Failed to parse response body")
|
||||
err = json.Unmarshal(usersRaw, &users)
|
||||
assert.NoErrorf(err, "Can not unmarshal users")
|
||||
require.NoErrorf(err, "Can not unmarshal users")
|
||||
|
||||
// Check existence of all known users
|
||||
for _, user := range TestUsers {
|
||||
|
@ -142,6 +144,7 @@ func TestApi_UpdateUser(t *testing.T) {
|
|||
|
||||
func TestApi_GetUser(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
require := require.New(t)
|
||||
|
||||
testCases := make(map[interface{}]tests.Request)
|
||||
testCases["abcde"] = tests.Request{Name: "Invalid id - string", Method: "GET", Endpoint: "/user/abcde", ShouldStatus: 500}
|
||||
|
@ -160,7 +163,7 @@ func TestApi_GetUser(t *testing.T) {
|
|||
|
||||
for id, testCase := range testCases {
|
||||
w, c, err := testCase.GetRequest()
|
||||
assert.NoErrorf(err, "(Test case %s) Could not make request", testCase.Name)
|
||||
require.NoErrorf(err, "(Test case %s) Could not make request", testCase.Name)
|
||||
|
||||
c.Set("id", id)
|
||||
TestUserHandler.GetUser(c)
|
||||
|
@ -171,9 +174,9 @@ func TestApi_GetUser(t *testing.T) {
|
|||
if testCase.ShouldReturn == 200 {
|
||||
user := &model.ExternalUser{}
|
||||
userBytes, err := ioutil.ReadAll(w.Body)
|
||||
assert.NoErrorf(err, "(Test case %s) can not read body", testCase.Name)
|
||||
require.NoErrorf(err, "(Test case %s) can not read body", testCase.Name)
|
||||
err = json.Unmarshal(userBytes, user)
|
||||
assert.NoErrorf(err, "(Test case %s) can not unmarshal body", testCase.Name)
|
||||
require.NoErrorf(err, "(Test case %s) can not unmarshal body", testCase.Name)
|
||||
|
||||
shouldUser, ok := testCase.ShouldReturn.(*model.User)
|
||||
assert.Truef(ok, "(Test case %s) successful response but no should response", testCase.Name)
|
||||
|
@ -189,6 +192,7 @@ func TestApi_GetUser(t *testing.T) {
|
|||
|
||||
func TestApi_DeleteUser(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
require := require.New(t)
|
||||
testCases := make(map[interface{}]tests.Request)
|
||||
testCases["abcde"] = tests.Request{Name: "Invalid user - string", Method: "DELETE", Endpoint: "/user/abcde", ShouldStatus: 500}
|
||||
testCases[uint(999999)] = tests.Request{Name: "Unknown user", Method: "DELETE", Endpoint: "/user/999999", ShouldStatus: 404}
|
||||
|
@ -205,7 +209,7 @@ func TestApi_DeleteUser(t *testing.T) {
|
|||
|
||||
for id, testCase := range testCases {
|
||||
w, c, err := testCase.GetRequest()
|
||||
assert.NoErrorf(err, "(Test case %s) Could not make request", testCase.Name)
|
||||
require.NoErrorf(err, "(Test case %s) Could not make request", testCase.Name)
|
||||
|
||||
c.Set("id", id)
|
||||
TestUserHandler.DeleteUser(c)
|
||||
|
|
|
@ -7,10 +7,12 @@ import (
|
|||
|
||||
"github.com/pushbits/server/tests"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestApi_SuccessOrAbort(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
require := require.New(t)
|
||||
|
||||
testCases := make(map[error]tests.Request)
|
||||
testCases[errors.New("")] = tests.Request{Name: "Empty Error - 500", Endpoint: "/", ShouldStatus: 500}
|
||||
|
@ -23,7 +25,7 @@ func TestApi_SuccessOrAbort(t *testing.T) {
|
|||
|
||||
for forcedErr, testCase := range testCases {
|
||||
w, c, err := testCase.GetRequest()
|
||||
assert.NoErrorf(err, "(Test case %s) Could not make request", testCase.Name)
|
||||
require.NoErrorf(err, "(Test case %s) Could not make request", testCase.Name)
|
||||
|
||||
aborted := successOrAbort(c, testCase.ShouldStatus, forcedErr)
|
||||
|
||||
|
@ -37,6 +39,7 @@ func TestApi_SuccessOrAbort(t *testing.T) {
|
|||
|
||||
func TestApi_IsCurrentUser(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
require := require.New(t)
|
||||
|
||||
for _, user := range TestUsers {
|
||||
testCases := make(map[uint]tests.Request)
|
||||
|
@ -46,7 +49,7 @@ func TestApi_IsCurrentUser(t *testing.T) {
|
|||
|
||||
for id, testCase := range testCases {
|
||||
w, c, err := testCase.GetRequest()
|
||||
assert.NoErrorf(err, "(Test case %s) Could not make request", testCase.Name)
|
||||
require.NoErrorf(err, "(Test case %s) Could not make request", testCase.Name)
|
||||
|
||||
c.Set("user", user)
|
||||
isCurrentUser := isCurrentUser(c, id)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue