cpularp-manager-api/test/unit/persontest/persontest.go

137 lines
4 KiB
Go
Raw Normal View History

package persontest
import (
"encoding/json"
"fmt"
"os"
group "example.com/database/group"
person "example.com/database/person"
testrequest "example.com/test/testrequest"
testsetup "example.com/test/testsetup"
grouptest "example.com/unit/grouptest"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/suite"
"gorm.io/gorm"
)
type TestPerson struct {
Result []person.Person `json:"result"`
}
type PersonTestSuite struct {
suite.Suite
globalAuthHeader string
router *gin.Engine
person person.Person
personJSON []byte
personUpdate person.Person
personUpdateJSON []byte
}
func (s *PersonTestSuite) SetupTestSuite() {
s.globalAuthHeader, s.router = testsetup.SetupTestSuite()
// Create Test Data
testGroup := group.Group{
Model: gorm.Model{ID: 1},
Name: "one",
}
testGroups := []group.Group{}
var updatedGroups []group.Group
updatedGroups = append(updatedGroups, testGroup)
s.person = person.Person{
Model: gorm.Model{ID: 2},
Name: "another_test",
Groups: testGroups,
}
s.personJSON, _ = json.Marshal(s.person)
s.personUpdate = person.Person{
Model: gorm.Model{ID: 2},
Name: "yet_another_test",
Groups: updatedGroups,
}
s.personUpdateJSON, _ = json.Marshal(person.PersonParams{
Name: s.personUpdate.Name,
Groups: []uint{1},
})
}
func (s *PersonTestSuite) TearDownTestSuite() {
os.Remove("db/main.db")
}
func (s *PersonTestSuite) Test01CreatePerson() {
// Setup variables
var output TestPerson
target := fmt.Sprintf("/person?id=%d", s.person.ID)
// Attempt to create person
status := testrequest.MakePostRequest(&s.personJSON, "/person", s.globalAuthHeader, s.router)
// Check that the request was successful
s.Equal(status, "200 OK", "person created successfully")
// Attempt to get person
body := testrequest.MakeGetRequest(target, s.router)
// Unmarshal the result
json.Unmarshal(body, &output)
// Check that the person listed matches the created person
s.Equal(s.person.Name, output.Result[0].Name, "input name matches output name")
}
func (s *PersonTestSuite) Test02GetPerson() {
// Setup variables
var output TestPerson
// Attempt to get persons
body := testrequest.MakeGetRequest("/person", s.router)
// Unmarshal the result
json.Unmarshal(body, &output)
// Check that the persons listed match the created persons
s.Equal(s.person.Name, output.Result[1].Name, "input name matches output name")
}
func (s *PersonTestSuite) Test03UpdatePerson() {
// Setup variables
var output TestPerson
target := fmt.Sprintf("/person?id=%d", s.person.ID)
// Create needed field objects
var groupOutput grouptest.TestGroup
groupBody, _ := json.Marshal(s.personUpdate.Groups[0])
groupStatus := testrequest.MakePostRequest(&groupBody, "/group", s.globalAuthHeader, s.router)
s.Equal(groupStatus, "200 OK", "group created successfully")
groupTarget := fmt.Sprintf("/group?id=%d", s.personUpdate.Groups[0].ID)
groupBody = testrequest.MakeGetRequest(groupTarget, s.router)
json.Unmarshal(groupBody, &groupOutput)
// Attempt to update person
status := testrequest.MakePutRequest(&s.personUpdateJSON, target, s.globalAuthHeader, s.router)
// Check that the request was successful
s.Equal(status, "200 OK", "person created successfully")
// Attempt to get person
body := testrequest.MakeGetRequest(target, s.router)
// Unmarshal the result
json.Unmarshal(body, &output)
// Check that the person listed matches the updated person
s.Equal(s.personUpdate.Name, output.Result[0].Name, "input name matches output name")
}
func (s *PersonTestSuite) Test04DeletePerson() {
// Setup variables
var output TestPerson
target := fmt.Sprintf("/person?id=%d", s.person.ID)
// Attempt to delete person
status := testrequest.MakeDeleteRequest(target, s.globalAuthHeader, s.router)
// Check that the request was successful
s.Equal(status, "200 OK", "person deleted successfully")
// Attempt to get person
body := testrequest.MakeGetRequest(target, s.router)
// Unmarshal the result
json.Unmarshal(body, &output)
// Check that the person list is empty
s.Equal(1, len(output.Result), "person list is empty")
}