Skip to content

Commit

Permalink
feat: add more tests (some failing, will fix)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZaguePrime committed Sep 28, 2024
1 parent 4ee4fe4 commit 5b4ad43
Showing 1 changed file with 233 additions and 0 deletions.
233 changes: 233 additions & 0 deletions api/handlers/ticket/ticket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package ticket_test
import (
"api/handlers/ticket"
"api/models"
"bytes"
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -51,6 +53,10 @@ func (suite *TicketErrorTestSuite) SetupTest() {
suite.router = router
}

func TestTicketErrors(t *testing.T) {
suite.Run(t, new(TicketErrorTestSuite))
}

/*-------------------------------MOCK MODELS-----------------------------------------*/
//Ticket Mocks

Expand Down Expand Up @@ -192,4 +198,231 @@ func (suite *TicketErrorTestSuite) TestCreateUnauthorized() {
suite.NotEmpty(result.Error)
}

func (suite *TicketErrorTestSuite) TestCreateUnauthorizedAdmin() {
suite.jwtMock.throwErrors = true
req, _ := http.NewRequest("POST", "/create", nil)
w := httptest.NewRecorder()
suite.router.ServeHTTP(w, req)

var result models.Response
suite.Equal(http.StatusUnauthorized, w.Code)
suite.NoError(json.Unmarshal(w.Body.Bytes(), &result))
suite.NotEmpty(result.Error)
}

func (suite *TicketErrorTestSuite) TestCreateBadRequest() {
req, _ := http.NewRequest("POST", "/create", nil)
w := httptest.NewRecorder()
suite.router.ServeHTTP(w, req)

var result models.Response
suite.Equal(http.StatusBadRequest, w.Code)
suite.NoError(json.Unmarshal(w.Body.Bytes(), &result))
suite.NotEmpty(result.Error)
}

func (suite *TicketErrorTestSuite) TestCreateError() {
suite.ticketMock.throwErrors = true

body := `{"dispute": 1, "subject": "test", "message": "test"}`
req, _ := http.NewRequest("POST", "/create", bytes.NewBuffer([]byte(body)))
w := httptest.NewRecorder()
suite.router.ServeHTTP(w, req)

var result models.Response
suite.Equal(http.StatusInternalServerError, w.Code)
suite.NoError(json.Unmarshal(w.Body.Bytes(), &result))
suite.NotEmpty(result.Error)
}

func (suite *TicketErrorTestSuite) TestCreateSuccess() {
body := `{"dispute": 1, "subject": "test", "message": "test"}`
req, _ := http.NewRequest("POST", "/create", bytes.NewBuffer([]byte(body)))
w := httptest.NewRecorder()
suite.router.ServeHTTP(w, req)

var result struct {
Data map[string]interface{} `json:"data"`
}
suite.Equal(http.StatusCreated, w.Code)
suite.NoError(json.Unmarshal(w.Body.Bytes(), &result))
suite.NotEmpty(result.Data)
}

// ---------------------------------------------------------------- CREATE TICKET MESSAGE
func (suite *TicketErrorTestSuite) TestCreateMessageUnauthorized() {
suite.jwtMock.throwErrors = true
req, _ := http.NewRequest("POST", "/1/messages", nil)
w := httptest.NewRecorder()
suite.router.ServeHTTP(w, req)

var result models.Response
suite.Equal(http.StatusUnauthorized, w.Code)
suite.NoError(json.Unmarshal(w.Body.Bytes(), &result))
suite.NotEmpty(result.Error)
}

func (suite *TicketErrorTestSuite) TestCreateTicketMessageBadID() {
body := `{"message": "test"}`
req, _ := http.NewRequest("POST", "/asdasdaw/messages", bytes.NewBuffer([]byte(body)))
w := httptest.NewRecorder()
suite.router.ServeHTTP(w, req)

var result models.Response
suite.Equal(http.StatusBadRequest, w.Code)
suite.NoError(json.Unmarshal(w.Body.Bytes(), &result))
suite.NotEmpty(result.Error)
}

func (suite *TicketErrorTestSuite) TestCreateTicketMessageError() {
suite.ticketMock.throwErrors = true
body := `{"message": "test"}`
req, _ := http.NewRequest("POST", "/1/messages", bytes.NewBuffer([]byte(body)))
w := httptest.NewRecorder()
suite.router.ServeHTTP(w, req)

var result models.Response
suite.Equal(http.StatusInternalServerError, w.Code)
suite.NoError(json.Unmarshal(w.Body.Bytes(), &result))
suite.NotEmpty(result.Error)
}

func (suite *TicketErrorTestSuite) TestCreateTicketSuccess() {
body := `{"message": "test"}`
req, _ := http.NewRequest("POST", "/1/messages", bytes.NewBuffer([]byte(body)))
w := httptest.NewRecorder()
suite.router.ServeHTTP(w, req)

var result struct {
Data map[string]interface{} `json:"data"`
}
suite.Equal(http.StatusCreated, w.Code)
suite.NoError(json.Unmarshal(w.Body.Bytes(), &result))
suite.NotEmpty(result.Data)
}

// ---------------------------------------------------------------- PATCH TICKET STATUS

func (suite *TicketErrorTestSuite) TestPatchUnauthorized() {
suite.jwtMock.throwErrors = true
req, _ := http.NewRequest("PATCH", "/1", nil)
w := httptest.NewRecorder()
suite.router.ServeHTTP(w, req)

var result models.Response
suite.Equal(http.StatusUnauthorized, w.Code)
suite.NoError(json.Unmarshal(w.Body.Bytes(), &result))
suite.NotEmpty(result.Error)
}

func (suite *TicketErrorTestSuite) TestPatchError() {
suite.ticketMock.throwErrors = true
req, _ := http.NewRequest("PATCH", "/1", nil)
w := httptest.NewRecorder()
suite.router.ServeHTTP(w, req)

var result models.Response
suite.Equal(http.StatusInternalServerError, w.Code)
suite.NoError(json.Unmarshal(w.Body.Bytes(), &result))
suite.NotEmpty(result.Error)
}

func (suite *TicketErrorTestSuite) TestPatchBadRequest() {

req, _ := http.NewRequest("PATCH", "/1", nil)
w := httptest.NewRecorder()
suite.router.ServeHTTP(w, req)

var result models.Response
suite.Equal(http.StatusBadRequest, w.Code)
suite.NoError(json.Unmarshal(w.Body.Bytes(), &result))
suite.NotEmpty(result.Error)
}

func (suite *TicketErrorTestSuite) TestPatchSuccess() {

body := `{"status": "test"}`
req, _ := http.NewRequest("PATCH", "/1", bytes.NewBuffer([]byte(body)))
w := httptest.NewRecorder()
suite.router.ServeHTTP(w, req)

suite.Equal(http.StatusNoContent, w.Code)
}

// ---------------------------------------------------------------- GET TICKET DETAILS

func (suite *TicketErrorTestSuite) TestGetTicketDetailsUnauthorized() {
suite.jwtMock.throwErrors = true
req, _ := http.NewRequest("GET", "/1", nil)
w := httptest.NewRecorder()
suite.router.ServeHTTP(w, req)

var result models.Response
suite.Equal(http.StatusUnauthorized, w.Code)
suite.NoError(json.Unmarshal(w.Body.Bytes(), &result))
suite.NotEmpty(result.Error)
}

func (suite *TicketErrorTestSuite) TestGetTicketDetailsError() {
suite.ticketMock.throwErrors = true
req, _ := http.NewRequest("GET", "/1", nil)
w := httptest.NewRecorder()
suite.router.ServeHTTP(w, req)

var result models.Response
suite.Equal(http.StatusInternalServerError, w.Code)
suite.NoError(json.Unmarshal(w.Body.Bytes(), &result))
suite.NotEmpty(result.Error)
}

func (suite *TicketErrorTestSuite) TestGetTicketDetailsSuccess() {
req, _ := http.NewRequest("GET", "/1", nil)
w := httptest.NewRecorder()
suite.router.ServeHTTP(w, req)

var result struct {
Data map[string]interface{} `json:"data"`
}
suite.Equal(http.StatusOK, w.Code)
suite.NoError(json.Unmarshal(w.Body.Bytes(), &result))
suite.NotEmpty(result.Data)
}

// ---------------------------------------------------------------- GET USER TICKET DETAILS

func (suite *TicketErrorTestSuite) TestGetUserTicketDetailsUnauthorized() {
suite.jwtMock.throwErrors = true
req, _ := http.NewRequest("GET", "/1", nil)
w := httptest.NewRecorder()
suite.router.ServeHTTP(w, req)

var result models.Response
suite.Equal(http.StatusUnauthorized, w.Code)
suite.NoError(json.Unmarshal(w.Body.Bytes(), &result))
suite.NotEmpty(result.Error)
}

func (suite *TicketErrorTestSuite) TestGetUserTicketDetailsError() {
suite.ticketMock.throwErrors = true
req, _ := http.NewRequest("GET", "/1", nil)
w := httptest.NewRecorder()
suite.router.ServeHTTP(w, req)

var result models.Response
suite.Equal(http.StatusInternalServerError, w.Code)
suite.NoError(json.Unmarshal(w.Body.Bytes(), &result))
suite.NotEmpty(result.Error)
}

func (suite *TicketErrorTestSuite) TestGetUserTicketDetailsSuccess() {
req, _ := http.NewRequest("GET", "/1", nil)
w := httptest.NewRecorder()
suite.router.ServeHTTP(w, req)

var result struct {
Data map[string]interface{} `json:"data"`
}
suite.Equal(http.StatusOK, w.Code)
suite.NoError(json.Unmarshal(w.Body.Bytes(), &result))
suite.NotEmpty(result.Data)
}

0 comments on commit 5b4ad43

Please sign in to comment.