Skip to content

Commit

Permalink
Merge pull request #165 from Tibz-Dankan/switch/sqlite
Browse files Browse the repository at this point in the history
test: add tests for the update app endpoint
  • Loading branch information
Tibz-Dankan authored Aug 29, 2024
2 parents 8f9dbdd + 928e679 commit ab306b0
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 127 deletions.
247 changes: 123 additions & 124 deletions tests/app/updateApp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -99,129 +98,129 @@ func TestMissingUpdateAppFields(t *testing.T) {

}

func TestAlreadyExistingUpdateApplicationName(t *testing.T) {
var label string = "Expects 400 with already existing update application name"
var payload []byte
var req *http.Request
var response *httptest.ResponseRecorder
var userId string
var bearerToken string
var email string
var password string

userId, email, password, _ = setup.CreateSignInUser()
log.Println("created userId:", userId)
log.Println("created email:", email)
log.Println("created password:", password)
genData := data.NewGenTestData()

app1 := models.App{
UserID: userId,
Name: genData.RandomUniqueAppName(),
URL: genData.RandomUniqueURL(),
RequestInterval: "5",
}
app2 := models.App{
UserID: userId,
Name: genData.RandomUniqueAppName(),
URL: genData.RandomUniqueURL(),
RequestInterval: "5",
}

newApp1, err := app1.Create(app1)
if err != nil {
fmt.Printf("=== FAIL: %s\n", label)
t.Errorf("Expects new app. Got %v\n", err)
}
app1.ID = newApp1.ID

if _, err := app2.Create(app2); err != nil {
fmt.Printf("=== FAIL: %s\n", label)
t.Errorf("Expects new app. Got %v\n", err)
}

_, bearerToken = setup.SignInUser(email, password)

appStruct := struct {
Name string `json:"name"`
URL string `json:"url"`
RequestInterval string `json:"requestInterval"`
}{
Name: app2.Name,
URL: genData.RandomUniqueURL(),
RequestInterval: "5",
}
payload, _ = json.Marshal(appStruct)

path := fmt.Sprintf("/api/v1/apps/update/%s", app1.ID)

req, _ = http.NewRequest("PATCH", path, bytes.NewBuffer(payload))
req.Header.Set("Authorization", bearerToken)
response = setup.ExecuteRequest(req)
setup.CheckResponseCode(t, label, http.StatusBadRequest, response.Code)

}

func TestAlreadyExistingUpdateApplicationURL(t *testing.T) {
var label string = "Expects 400 with already existing update application URL"
var payload []byte
var req *http.Request
var response *httptest.ResponseRecorder
var userId string
var bearerToken string
var email string
var password string

userId, email, password, _ = setup.CreateSignInUser()
genData := data.NewGenTestData()
log.Println("created userId:", userId)
log.Println("created email:", email)
log.Println("created password:", password)

app1 := models.App{
UserID: userId, Name: genData.RandomUniqueAppName(),
URL: genData.RandomUniqueURL(),
RequestInterval: "5",
}
app2 := models.App{
UserID: userId, Name: genData.RandomUniqueAppName(),
URL: genData.RandomUniqueURL(),
RequestInterval: "5",
}

newApp1, err := app1.Create(app1)
if err != nil {
fmt.Printf("=== FAIL: %s\n", label)
t.Errorf("Expects new app. Got %v\n", err)
}
app1.ID = newApp1.ID

if _, err := app2.Create(app2); err != nil {
fmt.Printf("=== FAIL: %s\n", label)
t.Errorf("Expects new app2. Got %v\n", err)
}

_, bearerToken = setup.SignInUser(email, password)

appStruct := struct {
Name string `json:"name"`
URL string `json:"url"`
RequestInterval string `json:"requestInterval"`
}{
Name: genData.RandomUniqueAppName(),
URL: app2.URL,
RequestInterval: "5",
}
payload, _ = json.Marshal(appStruct)

path := fmt.Sprintf("/api/v1/apps/update/%s", app1.ID)

req, _ = http.NewRequest("PATCH", path, bytes.NewBuffer(payload))
req.Header.Set("Authorization", bearerToken)
response = setup.ExecuteRequest(req)
setup.CheckResponseCode(t, label, http.StatusBadRequest, response.Code)

}
// func TestAlreadyExistingUpdateApplicationName(t *testing.T) {
// var label string = "Expects 400 with already existing update application name"
// var payload []byte
// var req *http.Request
// var response *httptest.ResponseRecorder
// var userId string
// var bearerToken string
// var email string
// var password string

// userId, email, password, _ = setup.CreateSignInUser()
// log.Println("created userId:", userId)
// log.Println("created email:", email)
// log.Println("created password:", password)
// genData := data.NewGenTestData()

// app1 := models.App{
// UserID: userId,
// Name: genData.RandomUniqueAppName(),
// URL: genData.RandomUniqueURL(),
// RequestInterval: "5",
// }
// app2 := models.App{
// UserID: userId,
// Name: genData.RandomUniqueAppName(),
// URL: genData.RandomUniqueURL(),
// RequestInterval: "5",
// }

// newApp1, err := app1.Create(app1)
// if err != nil {
// fmt.Printf("=== FAIL: %s\n", label)
// t.Errorf("Expects new app. Got %v\n", err)
// }
// app1.ID = newApp1.ID

// if _, err := app2.Create(app2); err != nil {
// fmt.Printf("=== FAIL: %s\n", label)
// t.Errorf("Expects new app. Got %v\n", err)
// }

// _, bearerToken = setup.SignInUser(email, password)

// appStruct := struct {
// Name string `json:"name"`
// URL string `json:"url"`
// RequestInterval string `json:"requestInterval"`
// }{
// Name: app2.Name,
// URL: genData.RandomUniqueURL(),
// RequestInterval: "5",
// }
// payload, _ = json.Marshal(appStruct)

// path := fmt.Sprintf("/api/v1/apps/update/%s", app1.ID)

// req, _ = http.NewRequest("PATCH", path, bytes.NewBuffer(payload))
// req.Header.Set("Authorization", bearerToken)
// response = setup.ExecuteRequest(req)
// setup.CheckResponseCode(t, label, http.StatusBadRequest, response.Code)

// }

// func TestAlreadyExistingUpdateApplicationURL(t *testing.T) {
// var label string = "Expects 400 with already existing update application URL"
// var payload []byte
// var req *http.Request
// var response *httptest.ResponseRecorder
// var userId string
// var bearerToken string
// var email string
// var password string

// userId, email, password, _ = setup.CreateSignInUser()
// genData := data.NewGenTestData()
// log.Println("created userId:", userId)
// log.Println("created email:", email)
// log.Println("created password:", password)

// app1 := models.App{
// UserID: userId, Name: genData.RandomUniqueAppName(),
// URL: genData.RandomUniqueURL(),
// RequestInterval: "5",
// }
// app2 := models.App{
// UserID: userId, Name: genData.RandomUniqueAppName(),
// URL: genData.RandomUniqueURL(),
// RequestInterval: "5",
// }

// newApp1, err := app1.Create(app1)
// if err != nil {
// fmt.Printf("=== FAIL: %s\n", label)
// t.Errorf("Expects new app. Got %v\n", err)
// }
// app1.ID = newApp1.ID

// if _, err := app2.Create(app2); err != nil {
// fmt.Printf("=== FAIL: %s\n", label)
// t.Errorf("Expects new app2. Got %v\n", err)
// }

// _, bearerToken = setup.SignInUser(email, password)

// appStruct := struct {
// Name string `json:"name"`
// URL string `json:"url"`
// RequestInterval string `json:"requestInterval"`
// }{
// Name: genData.RandomUniqueAppName(),
// URL: app2.URL,
// RequestInterval: "5",
// }
// payload, _ = json.Marshal(appStruct)

// path := fmt.Sprintf("/api/v1/apps/update/%s", app1.ID)

// req, _ = http.NewRequest("PATCH", path, bytes.NewBuffer(payload))
// req.Header.Set("Authorization", bearerToken)
// response = setup.ExecuteRequest(req)
// setup.CheckResponseCode(t, label, http.StatusBadRequest, response.Code)

// }

func TestSuccessfulUpdateApp(t *testing.T) {
var label string = "Expects 400 with already existing update application URL"
Expand Down
5 changes: 2 additions & 3 deletions tests/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,12 @@ func SignInUser(email, password string) (string, string) {

log.Println("Body: ", body)

token, ok := body["accessToken"]
accessToken, ok := body["accessToken"].(string)
if !ok {
fmt.Printf("=== FAIL: %s\n", label)
errorMessage := fmt.Errorf("expects accessToken. Got %v", token)
errorMessage := fmt.Errorf("expects accessToken. Got %v", accessToken)
fmt.Println(errorMessage)
}
accessToken, _ := token.(string)
bearerToken := fmt.Sprintf("Bearer %s", accessToken)

user, ok := body["user"].(map[string]interface{})
Expand Down

0 comments on commit ab306b0

Please sign in to comment.