mirror of
https://github.com/pushbits/server.git
synced 2025-05-10 15:37:00 +02:00
add testing for util.go
This commit is contained in:
parent
562ee7aa4a
commit
a6fa829165
2 changed files with 63 additions and 1 deletions
|
@ -184,7 +184,6 @@ func TestApi_GetUser(t *testing.T) {
|
|||
assert.Equalf(user.MatrixID, shouldUser.MatrixID, "(Test case %s) user matrix ID should be %s but is %s", testCase.Name, user.MatrixID, shouldUser.MatrixID)
|
||||
assert.Equalf(user.IsAdmin, shouldUser.IsAdmin, "(Test case %s) user is admin should be %v but is %v", testCase.Name, user.IsAdmin, shouldUser.IsAdmin)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
63
internal/api/util_test.go
Normal file
63
internal/api/util_test.go
Normal file
|
@ -0,0 +1,63 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/pushbits/server/tests"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestApi_SuccessOrAbort(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
testCases := make(map[error]tests.Request)
|
||||
testCases[errors.New("")] = tests.Request{Name: "Empty Error - 500", Endpoint: "/", ShouldStatus: 500}
|
||||
testCases[errors.New("this is an error")] = tests.Request{Name: "Error - 500", Endpoint: "/", ShouldStatus: 500}
|
||||
testCases[errors.New("this is an error")] = tests.Request{Name: "Error - 200", Endpoint: "/", ShouldStatus: 200}
|
||||
testCases[errors.New("this is an error")] = tests.Request{Name: "Error - 404", Endpoint: "/", ShouldStatus: 404}
|
||||
testCases[errors.New("this is an error")] = tests.Request{Name: "Error - 1001", Endpoint: "/", ShouldStatus: 1001}
|
||||
testCases[nil] = tests.Request{Name: "No Error - 1001", Endpoint: "/", ShouldStatus: 1001}
|
||||
testCases[nil] = tests.Request{Name: "No Error - 404", Endpoint: "/", ShouldStatus: 404}
|
||||
|
||||
for forcedErr, testCase := range testCases {
|
||||
w, c, err := testCase.GetRequest()
|
||||
assert.NoErrorf(err, "(Test case %s) Could not make request", testCase.Name)
|
||||
|
||||
aborted := successOrAbort(c, testCase.ShouldStatus, forcedErr)
|
||||
|
||||
if forcedErr != nil {
|
||||
assert.Equalf(testCase.ShouldStatus, w.Code, "(Test case %s) should return code %d but returned %d", testCase.Name, testCase.ShouldStatus, w.Code)
|
||||
}
|
||||
|
||||
assert.Equalf(forcedErr == nil, aborted, "(Test case %s) should return %v but returned %v", testCase.Name, forcedErr == nil, aborted)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApi_IsCurrentUser(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
for _, user := range TestUsers {
|
||||
testCases := make(map[uint]tests.Request)
|
||||
|
||||
testCases[user.ID] = tests.Request{Name: fmt.Sprintf("User %s - success", user.Name), Endpoint: "/", ShouldStatus: 200}
|
||||
testCases[uint(49786749859)] = tests.Request{Name: fmt.Sprintf("User %s - fail", user.Name), Endpoint: "/", ShouldStatus: 403}
|
||||
|
||||
for id, testCase := range testCases {
|
||||
w, c, err := testCase.GetRequest()
|
||||
assert.NoErrorf(err, "(Test case %s) Could not make request", testCase.Name)
|
||||
|
||||
c.Set("user", user)
|
||||
isCurrentUser := isCurrentUser(c, id)
|
||||
|
||||
if testCase.ShouldStatus == 200 {
|
||||
assert.Truef(isCurrentUser, "(Test case %s) should be true but is false", testCase.Name)
|
||||
} else {
|
||||
assert.Falsef(isCurrentUser, "(Test case %s) should be false but is true", testCase.Name)
|
||||
assert.Equalf(w.Code, testCase.ShouldStatus, "(Test case %s) should return status %d but returned %d", testCase.Name, testCase.ShouldStatus, w.Code)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue