Adding tests for Product endpoints

parent 47473205
......@@ -358,7 +358,7 @@ func APIv1GetCrashes(c *gin.Context) {
func APIv1GetCrash(c *gin.Context) {
var Crash database.Crash
database.Db.First(&Crash, "id = ?", c.Param("id"))
c.JSON(http.StatusOK, Crash)
c.JSON(http.StatusOK, gin.H{"Item": Crash, "Error": nil})
}
// APIv1GetReports is the GET endpoint for reports in API v1
......@@ -376,7 +376,7 @@ func APIv1GetReports(c *gin.Context) {
func APIv1GetReport(c *gin.Context) {
var Report database.Report
database.Db.First(&Report, "id = ?", c.Param("id"))
c.JSON(http.StatusOK, Report)
c.JSON(http.StatusOK, gin.H{"Item": Report, "Error": nil})
}
// APIv1GetSymfiles is the GET endpoint for symfiles in API v1
......@@ -394,7 +394,7 @@ func APIv1GetSymfiles(c *gin.Context) {
func APIv1GetSymfile(c *gin.Context) {
var Symfile database.Symfile
database.Db.First(&Symfile, "id = ?", c.Param("id"))
c.JSON(http.StatusOK, Symfile)
c.JSON(http.StatusOK, gin.H{"Item": Symfile, "Error": nil})
}
// APIv1GetProducts is the GET endpoint for products in API v1
......@@ -412,7 +412,7 @@ func APIv1GetProducts(c *gin.Context) {
func APIv1GetProduct(c *gin.Context) {
var Product database.Product
database.Db.First(&Product, "id = ?", c.Param("id"))
c.JSON(http.StatusOK, Product)
c.JSON(http.StatusOK, gin.H{"Item": Product, "Error": nil})
}
// APIv1NewProduct processes the new product endpoint
......@@ -476,7 +476,7 @@ func APIv1GetVersions(c *gin.Context) {
func APIv1GetVersion(c *gin.Context) {
var Version database.Version
database.Db.First(&Version, "id = ?", c.Param("id"))
c.JSON(http.StatusOK, Version)
c.JSON(http.StatusOK, gin.H{"Item": Version, "Error": nil})
}
// APIv1NewVersion processes the new product form
......@@ -543,7 +543,7 @@ func APIv1GetUsers(c *gin.Context) {
func APIv1GetUser(c *gin.Context) {
var User database.User
database.Db.First(&User, "id = ?", c.Param("id"))
c.JSON(http.StatusOK, User)
c.JSON(http.StatusOK, gin.H{"Item": User, "Error": nil})
}
// APIv1NewUser processes the new user endpoint
......@@ -607,7 +607,7 @@ func APIv1GetComments(c *gin.Context) {
func APIv1GetComment(c *gin.Context) {
var Comment database.Comment
database.Db.First(&Comment, "id = ?", c.Param("id"))
c.JSON(http.StatusOK, Comment)
c.JSON(http.StatusOK, gin.H{"Item": Comment, "Error": nil})
}
// APIv1NewComment processes the new comment endpoint
......
package main
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"strings"
"code.videolan.org/videolan/CrashDragon/database"
)
type apiProdResp struct {
Error string
Item database.Product
}
type apiProdListResp struct {
Error string
Items []database.Product
}
func testProduct() {
client := &http.Client{}
var Product database.Product
Product.Name = "Testproduct 1"
Product.Slug = "tp1"
j, err := json.Marshal(Product)
if err != nil {
log.Panic(err)
}
// Test POST
res, err := http.Post("http://admin:12345@127.0.0.1:8080/api/v1/products", "application/json", strings.NewReader(string(j)))
if err != nil {
log.Panic(err)
}
s, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Panic(err)
}
var Product2 apiProdResp
err = json.Unmarshal(s, &Product2)
if err != nil {
log.Panic(err)
}
if Product2.Item.Name == Product.Name && Product2.Item.Slug == Product.Slug {
log.Println("Product POSTed with ID", Product2.Item.ID.String())
} else {
log.Println("Should:", Product)
log.Println("Is:", Product2.Item)
log.Fatalln("Product could not be POSTed, error:", Product2.Error)
}
// Test LIST
res2, err := http.Get("http://admin:12345@127.0.0.1:8080/api/v1/products")
if err != nil {
log.Panic(err)
}
s2, err := ioutil.ReadAll(res2.Body)
if err != nil {
log.Panic(err)
}
var Product3 apiProdListResp
err = json.Unmarshal(s2, &Product3)
if err != nil {
log.Panic(err)
}
found := false
for _, prod := range Product3.Items {
if prod.Name == Product.Name && prod.Slug == Product.Slug {
found = true
log.Println("Product found with ID", prod.ID.String())
}
}
if found == false {
log.Println("Should:", Product)
log.Println("Is:", Product3.Items)
log.Fatalln("Product could not be found, error:", Product3.Error)
}
// Test GET
res3, err := http.Get("http://admin:12345@127.0.0.1:8080/api/v1/products/" + Product2.Item.ID.String())
if err != nil {
log.Panic(err)
}
s3, err := ioutil.ReadAll(res3.Body)
if err != nil {
log.Panic(err)
}
var Product4 apiProdResp
err = json.Unmarshal(s3, &Product4)
if err != nil {
log.Panic(err)
}
if Product4.Item.Name == Product.Name && Product4.Item.Slug == Product.Slug {
log.Println("Product GOT with ID", Product4.Item.ID.String())
} else {
log.Println("Should:", Product)
log.Println("Is:", Product4.Item)
log.Fatalln("Product could not be GOT, error:", Product4.Error)
}
// Test PUT
Product.Name = "Testproduct 2"
Product.Slug = "tp2"
j, err = json.Marshal(Product)
if err != nil {
log.Panic(err)
}
req, err := http.NewRequest("PUT", "http://admin:12345@127.0.0.1:8080/api/v1/products/"+Product2.Item.ID.String(), strings.NewReader(string(j)))
if err != nil {
log.Panic(err)
}
res4, err := client.Do(req)
if err != nil {
log.Panic(err)
}
s4, err := ioutil.ReadAll(res4.Body)
if err != nil {
log.Panic(err)
}
var Product5 apiProdResp
err = json.Unmarshal(s4, &Product5)
if err != nil {
log.Panic(err)
}
if Product5.Item.Name == Product.Name && Product5.Item.Slug == Product.Slug {
log.Println("Product PUTed with ID", Product5.Item.ID.String())
} else {
log.Println("Should:", Product)
log.Println("Is:", Product5.Item)
log.Fatalln("Product could not be PUTed, error:", Product5.Error)
}
// Test DELETE
req2, err := http.NewRequest("DELETE", "http://admin:12345@127.0.0.1:8080/api/v1/products/"+Product2.Item.ID.String(), nil)
if err != nil {
log.Panic(err)
}
res5, err := client.Do(req2)
if err != nil {
log.Panic(err)
}
s5, err := ioutil.ReadAll(res5.Body)
if err != nil {
log.Panic(err)
}
var Product6 apiProdResp
err = json.Unmarshal(s5, &Product6)
if err != nil {
log.Panic(err)
}
if Product6.Error == "" {
log.Println("Product DELETEd with ID", Product2.Item.ID.String())
} else {
log.Fatalln("Product could not be DELETEd, error:", Product6.Error)
}
}
func runTests() {
testProduct()
}
func main() {
log.Println("Running CrashDragon tests...")
runTests()
log.Println("All tests finished successfully")
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment