func TestAPI(t *testing.T) { go main() api := forest.NewClient("http://localhost:8888", new(http.Client)) r := api.POST(t, forest.Path("/api/shorten").Body("https://github.com/stefanteixeira")) r2 := api.POST(t, forest.Path("/api/shorten").Body("https://github.com/stefanteixeira")) assert.Equal(t, r.StatusCode, 201) assert.Equal(t, r2.StatusCode, 200) }
func TestForestCreateOrderInvalidTime(t *testing.T) { invalidTimes := []uint64{ // 0, // valid // uint64(time.Now().UTC().Unix()), // valid uint64(time.Now().UTC().Add(-(engine.PastTimeTreshold + time.Second)).Unix()), } for _, invalidTime := range invalidTimes { payload := data.Order{ Time: invalidTime, } got := Error{} r := getForestClient().POST(t, forest.Path("/orders/{}", "").Content(payload, MimeJSON)) // TODO: // forest.ExpectStatus(t, r, http.StatusBadRequest) forest.ExpectStatus(t, r, http.StatusInternalServerError) forest.ExpectJSONDocument(t, r, &got) if got.Message == "" { t.Error("Got empty error message") } } }
func TestForestListOrders(t *testing.T) { expected := []data.Order{} for i := 1; i <= 3; i++ { _time := uint64(time.Now().UTC().Unix()) order := data.Order{ Time: _time, } createOrder(&order) // newest come first: expected = append([]data.Order{order}, expected...) } got := []data.Order{} r := getForestClient().GET(t, forest.Path("/orders/{}", "").Query("limit", len(expected))) forest.ExpectStatus(t, r, http.StatusOK) forest.ExpectJSONDocument(t, r, &got) if !reflect.DeepEqual(got, expected) { t.Errorf("Got: %s\n\nExpected: %s", testutils.JSON(got), testutils.JSON(expected)) } }
func TestForestRemoveOrderItemNonExisting(t *testing.T) { product := data.Product{ Name: generateName("Product"), Price: data.Price{ Vat: 20, Net: 100, }, } createProduct(&product) order := data.Order{} createOrder(&order) got := Error{} r := getForestClient().DELETE(t, forest.Path("/orders/{orderID}/items/{productID}", order.ID, product.ID)) // TODO: // forest.ExpectStatus(t, r, http.StatusNotFound) forest.ExpectStatus(t, r, http.StatusInternalServerError) forest.ExpectJSONDocument(t, r, &got) if got.Message == "" { t.Error("Got empty error message") } }
func TestForestOrderItemCreateNonExistingProduct(t *testing.T) { nonExistingProductID := uint64(1000000) // TODO order := data.Order{} createOrder(&order) payload := data.OrderItem{ Product: data.Product{ ID: nonExistingProductID, }, Quantity: 1, } got := Error{} r := getForestClient().POST(t, forest.Path("/orders/{orderID}/items/{}", order.ID, "").Content(payload, MimeJSON)) // TODO: // forest.ExpectStatus(t, r, http.StatusNotFound) forest.ExpectStatus(t, r, http.StatusInternalServerError) forest.ExpectJSONDocument(t, r, &got) if got.Message == "" { t.Error("Got empty error message") } }
func TestForestRemoveOrderItem(t *testing.T) { product := data.Product{ Name: generateName("Product"), Price: data.Price{ Vat: 20, Net: 100, }, } createProduct(&product) order := data.Order{ Items: []data.OrderItem{ data.OrderItem{ Product: product, Quantity: 1, }, }, } createOrder(&order) r := getForestClient().DELETE(t, forest.Path("/orders/{orderID}/items/{itemID}", order.ID, product.ID)) forest.ExpectStatus(t, r, http.StatusNoContent) }
func TestForestUpdateOrder(t *testing.T) { _time := uint64(time.Now().UTC().Unix()) _newTime := _time + 300 // 5 min order := data.Order{ Time: _time, } createOrder(&order) expected := order expected.Time = _newTime payload := data.Order{ Time: _newTime, } got := data.Order{} r := getForestClient().PUT(t, forest.Path("/orders/{id}", order.ID).Content(payload, MimeJSON)) forest.ExpectStatus(t, r, http.StatusOK) forest.ExpectJSONDocument(t, r, &got) expected.ID = got.ID if !reflect.DeepEqual(got, expected) { t.Errorf("Got: %s\n\nExpected: %s", testutils.JSON(got), testutils.JSON(expected)) } }
func TestForestUpdateOrderDraftCancelled(t *testing.T) { order := data.Order{} createOrder(&order) expected := order expected.State = data.OrderStateCancelled payload := data.Order{ State: data.OrderStateCancelled, } got := data.Order{} r := getForestClient().PUT(t, forest.Path("/orders/{id}", order.ID).Content(payload, MimeJSON)) forest.ExpectStatus(t, r, http.StatusOK) forest.ExpectJSONDocument(t, r, &got) expected.ID = got.ID if !reflect.DeepEqual(got, expected) { t.Errorf("Got: %s\n\nExpected: %s", testutils.JSON(got), testutils.JSON(expected)) } }
func TestForestCreateOrder(t *testing.T) { _time := uint64(time.Now().UTC().Unix()) expected := data.Order{ State: data.OrderStateDraft, Time: _time, } populate.Order(&expected) payload := data.Order{ Time: _time, } got := data.Order{} r := getForestClient().POST(t, forest.Path("/orders/{}", "").Content(payload, MimeJSON)) forest.ExpectStatus(t, r, http.StatusCreated) forest.ExpectJSONDocument(t, r, &got) expected.ID = got.ID if !reflect.DeepEqual(got, expected) { t.Errorf("Got: %s\n\nExpected: %s", testutils.JSON(got), testutils.JSON(expected)) } }
func TestForestOrderItemCreateOrderPlaced(t *testing.T) { productInOrder := data.Product{ Name: generateName("Product"), Price: data.Price{ Vat: 20, Net: 100, }, } createProduct(&productInOrder) product := data.Product{ Name: generateName("Product"), Price: data.Price{ Vat: 20, Net: 100, }, } createProduct(&product) order := data.Order{ Items: []data.OrderItem{ data.OrderItem{ Product: productInOrder, Quantity: 1, }, }, } createOrder(&order) order.State = data.OrderStatePlaced updateOrder(&order) payload := data.OrderItem{ Product: data.Product{ ID: product.ID, }, Quantity: 0, } got := Error{} r := getForestClient().POST(t, forest.Path("/orders/{orderID}/items/{}", order.ID, "").Content(payload, MimeJSON)) // TODO: // forest.ExpectStatus(t, r, http.StatusForbidden) forest.ExpectStatus(t, r, http.StatusInternalServerError) forest.ExpectJSONDocument(t, r, &got) if got.Message == "" { t.Error("Got empty error message") } }
func TestForestCreateOrderWithItems(t *testing.T) { _time := uint64(time.Now().UTC().Unix()) expected := data.Order{ State: data.OrderStateDraft, Time: _time, } payload := data.Order{ Time: _time, } for i := 1; i <= 3; i++ { product := data.Product{ Name: generateName("Product"), Price: data.Price{ Vat: 20, Net: uint64(100 * i), }, } createProduct(&product) item := data.OrderItem{ Product: product, Quantity: uint64(i), } // newest come first: payload.Items = append([]data.OrderItem{item}, payload.Items...) expected.Items = append([]data.OrderItem{item}, expected.Items...) } populate.Order(&expected) got := data.Order{} r := getForestClient().POST(t, forest.Path("/orders/{}", "").Content(payload, MimeJSON)) forest.ExpectStatus(t, r, http.StatusCreated) forest.ExpectJSONDocument(t, r, &got) expected.ID = got.ID if !reflect.DeepEqual(got, expected) { t.Errorf("Got: %s\n\nExpected: %s", testutils.JSON(got), testutils.JSON(expected)) } }
func TestForestOrderItemUpdate(t *testing.T) { product := data.Product{ Name: generateName("Product"), Price: data.Price{ Vat: 20, Net: 100, }, } createProduct(&product) order := data.Order{ Items: []data.OrderItem{ data.OrderItem{ Product: product, Quantity: 1, }, }, } createOrder(&order) expected := data.OrderItem{ Product: product, Quantity: 20, } populate.OrderItem(&expected) payload := data.OrderItem{ Product: data.Product{ ID: product.ID, }, Quantity: 20, } got := data.OrderItem{} r := getForestClient().PUT(t, forest.Path("/orders/{orderID}/items/{productID}", order.ID, product.ID).Content(payload, MimeJSON)) forest.ExpectStatus(t, r, http.StatusOK) forest.ExpectJSONDocument(t, r, &got) if !reflect.DeepEqual(got, expected) { t.Errorf("Got: %s\n\nExpected: %s", testutils.JSON(got), testutils.JSON(expected)) } }
func TestForestUpdateOrderDraftPlaced(t *testing.T) { product := data.Product{ Name: generateName("Product"), Price: data.Price{ Vat: 20, Net: 100, }, } createProduct(&product) order := data.Order{ Items: []data.OrderItem{ data.OrderItem{ Product: product, Quantity: 1, }, }, } createOrder(&order) expected := order expected.State = data.OrderStatePlaced payload := data.Order{ State: data.OrderStatePlaced, } got := data.Order{} r := getForestClient().PUT(t, forest.Path("/orders/{id}", order.ID).Content(payload, MimeJSON)) forest.ExpectStatus(t, r, http.StatusOK) forest.ExpectJSONDocument(t, r, &got) expected.ID = got.ID if !reflect.DeepEqual(got, expected) { t.Errorf("Got: %s\n\nExpected: %s", testutils.JSON(got), testutils.JSON(expected)) } }
func TestForestLoadOrderNonExisting(t *testing.T) { nonExistingOrderID := 1000000 // TODO got := Error{} r := getForestClient().GET(t, forest.Path("/orders/{id}", nonExistingOrderID)) // TODO: // forest.ExpectStatus(t, r, http.StatusNotFound) forest.ExpectStatus(t, r, http.StatusInternalServerError) forest.ExpectJSONDocument(t, r, &got) if got.Message == "" { t.Error("Got empty error message") } }
func TestForestListOrderItems(t *testing.T) { order := data.Order{} for i := 1; i <= 3; i++ { product := data.Product{ Name: generateName("Product"), Price: data.Price{ Vat: 20, Net: 100, }, } createProduct(&product) item := data.OrderItem{ Product: product, Quantity: 1, } order.Items = append([]data.OrderItem{item}, order.Items...) } createOrder(&order) expected := order.Items got := []data.OrderItem{} r := getForestClient().GET(t, forest.Path("/orders/{orderID}/items/{}", order.ID, "").Query("limit", len(expected))) forest.ExpectStatus(t, r, http.StatusOK) forest.ExpectJSONDocument(t, r, &got) if !reflect.DeepEqual(got, expected) { t.Errorf("Got: %s\n\nExpected: %s", testutils.JSON(got), testutils.JSON(expected)) } }
func TestForestOrderItemUpdateNonExisting(t *testing.T) { product := data.Product{ Name: generateName("Product"), Price: data.Price{ Vat: 20, Net: 100, }, } createProduct(&product) order := data.Order{} createOrder(&order) payload := data.OrderItem{ Product: data.Product{ ID: product.ID, }, Quantity: 20, } got := Error{} r := getForestClient().PUT(t, forest.Path("/orders/{orderID}/items/{productID}", order.ID, product.ID).Content(payload, MimeJSON)) // TODO: // forest.ExpectStatus(t, r, http.StatusNotFound) forest.ExpectStatus(t, r, http.StatusInternalServerError) forest.ExpectJSONDocument(t, r, &got) if got.Message == "" { t.Error("Got empty error message") } }
func TestForestLoadOrder(t *testing.T) { _time := uint64(time.Now().UTC().Unix()) expected := data.Order{ Time: _time, } createOrder(&expected) got := data.Order{} r := getForestClient().GET(t, forest.Path("/orders/{id}", expected.ID)) forest.ExpectStatus(t, r, http.StatusOK) forest.ExpectJSONDocument(t, r, &got) if !reflect.DeepEqual(got, expected) { t.Errorf("Got: %s\n\nExpected: %s", testutils.JSON(got), testutils.JSON(expected)) } }
func TestForestCreateOrderInvalidState(t *testing.T) { invalidStates := []string{ // "", // valid // data.OrderStateDraft, // valid data.OrderStatePlaced, data.OrderStateCancelled, "UNKNOWN", } for _, invalidState := range invalidStates { _time := uint64(time.Now().UTC().Unix()) payload := data.Order{ State: invalidState, Time: _time, } got := Error{} r := getForestClient().POST(t, forest.Path("/orders/{}", "").Content(payload, MimeJSON)) // TODO: // forest.ExpectStatus(t, r, http.StatusBadRequest) forest.ExpectStatus(t, r, http.StatusInternalServerError) forest.ExpectJSONDocument(t, r, &got) if got.Message == "" { t.Error("Got empty error message") } } }
func TestForestCreateOrderInvalidItems(t *testing.T) { _time := uint64(time.Now().UTC().Unix()) product := data.Product{ Name: generateName("Product"), Price: data.Price{ Vat: 20, Net: 100, }, } invalidOrderItemSets := [][]data.OrderItem{ // non-existing products: []data.OrderItem{ data.OrderItem{ Product: data.Product{ ID: 1000000, // TODO }, Quantity: 1, }, }, // duplicated products: []data.OrderItem{ data.OrderItem{ Product: data.Product{ ID: product.ID, }, Quantity: 1, }, data.OrderItem{ Product: data.Product{ ID: product.ID, }, Quantity: 1, }, }, // zero quantity: []data.OrderItem{ data.OrderItem{ Product: data.Product{ ID: product.ID, }, Quantity: 0, }, }, } for _, invalidOrderItemSet := range invalidOrderItemSets { payload := data.Order{ Time: _time, Items: invalidOrderItemSet, } got := Error{} r := getForestClient().POST(t, forest.Path("/orders/{}", "").Content(payload, MimeJSON)) // TODO: // forest.ExpectStatus(t, r, http.StatusBadRequest) forest.ExpectStatus(t, r, http.StatusInternalServerError) forest.ExpectJSONDocument(t, r, &got) if got.Message == "" { t.Error("Got empty error message") } } }
func TestForestUpdateOrderInvalidStateTransition(t *testing.T) { invalidTransitions := [][2]string{ // [2]string{data.OrderStateDraft, data.OrderStatePlaced}, // valid // [2]string{data.OrderStateDraft, data.OrderStateCancelled}, // valid [2]string{data.OrderStatePlaced, data.OrderStateDraft}, [2]string{data.OrderStatePlaced, data.OrderStateCancelled}, [2]string{data.OrderStateCancelled, data.OrderStateDraft}, [2]string{data.OrderStateCancelled, data.OrderStatePlaced}, [2]string{data.OrderStateDraft, "UNKNOWN"}, [2]string{data.OrderStatePlaced, "UNKNOWN"}, [2]string{data.OrderStateCancelled, "UNKNOWN"}, } product := data.Product{ Name: generateName("Product"), Price: data.Price{ Vat: 20, Net: 100, }, } createProduct(&product) for _, invalidTransition := range invalidTransitions { fromState, toState := invalidTransition[0], invalidTransition[1] order := data.Order{ Items: []data.OrderItem{ data.OrderItem{ Product: product, Quantity: 1, }, }, } createOrder(&order) if fromState != order.State { order.State = fromState updateOrder(&order) } payload := data.Order{ State: toState, } got := Error{} r := getForestClient().PUT(t, forest.Path("/orders/{id}", order.ID).Content(payload, MimeJSON)) // TODO: // forest.ExpectStatus(t, r, http.StatusBadRequest) forest.ExpectStatus(t, r, http.StatusInternalServerError) forest.ExpectJSONDocument(t, r, &got) if got.Message == "" { t.Error("Got empty error message") } } }