Exemplo n.º 1
0
func TestAuthWrongPin(t *testing.T) {
	fake := FakeAuthenticator("123456")
	h := &auth{fake}

	cases := []struct {
		in  string
		msg string
	}{
		{`{"pin":"654321"}`, "auth: sent wrong pin, expect 400, got %d"},
		{`{}`, "auth: sent no pin, expect 400, got %d"},
		{`"123456"`, "auth: sent wrong format, expect 400, got %d"},
	}

	for _, c := range cases {
		resp, err := jsonapi.HandlerTest(jsonapi.APIHandler(h.Handle).Handler).Post("/api/auth", "", c.in)

		if err != nil {
			t.Fatalf("unexpected error occured when testing auth: %s", err)
		}

		if resp.Code != http.StatusBadRequest {
			t.Errorf(c.msg, resp.Code)
		}
	}
}
Exemplo n.º 2
0
func TestListAll(t *testing.T) {
	presetData := []Order{
		Order{1468248039, 100, -100, "USD"},
		Order{1468248040, -50, 51, "USD"},
		Order{1468248041, 100, -100, "JPY"},
		Order{1468248042, -50, 51, "JPY"},
	}
	h, token, mgr := makeListAll(presetData)
	defer mgr.Connection().Close()

	resp, err := jsonapi.HandlerTest(jsonapi.APIHandler(h.Handle).Handler).Post("/api/listall", "", `{"token":"`+token+`"}`)

	if err != nil {
		t.Fatalf("unexpected error occured when testing listall: %s", err)
	}

	if resp.Code != http.StatusOK {
		t.Fatalf("unexpected error occured when testing listall with status code %d: %s", resp.Code, resp.Body.String())
	}

	var orders []Order
	if resp.Body == nil {
		t.Fatal(resp)
	}
	if err := json.Unmarshal(resp.Body.Bytes(), &orders); err != nil {
		t.Fatalf("cannot encode returned data from listall: %s", err)
	}

	msgs := validateOrders(presetData, orders)
	for _, msg := range msgs {
		t.Errorf("listall: %s", msg)
	}
}
Exemplo n.º 3
0
func TestListAllWrongToken(t *testing.T) {
	h, _, mgr := makeListAll([]Order{})
	defer mgr.Connection().Close()

	resp, err := jsonapi.HandlerTest(jsonapi.APIHandler(h.Handle).Handler).Post("/api/listall", "", `{"token":"1234"}`)

	if err != nil {
		t.Fatalf("unexpected error occured when testing listall: %s", err)
	}

	if resp.Code != http.StatusForbidden {
		t.Fatalf("expected listall return forbidden when wrong token, got %d: %s", resp.Code, resp.Body.String())
	}
}
Exemplo n.º 4
0
func TestListAllNotJSON(t *testing.T) {
	h, _, mgr := makeListAll([]Order{})
	defer mgr.Connection().Close()

	resp, err := jsonapi.HandlerTest(jsonapi.APIHandler(h.Handle).Handler).Post("/api/listall", "", `1234`)

	if err != nil {
		t.Fatalf("unexpected error occured when testing listall: %s", err)
	}

	if resp.Code != http.StatusBadRequest {
		t.Fatalf("expected listall return bad request when not sending json, got %d: %s", resp.Code, resp.Body.String())
	}
}
Exemplo n.º 5
0
func TestAddNoData(t *testing.T) {
	h, token, mgr := makeAdd([]Order{})
	defer mgr.Connection().Close()

	resp, err := jsonapi.HandlerTest(jsonapi.APIHandler(h.Handle).Handler).Post("/api/add", "", `{"token":"`+token+`"}`)

	if err != nil {
		t.Fatalf("unexpected error occured when testing add: %s", err)
	}

	if resp.Code != http.StatusBadRequest {
		t.Fatalf("unexpected status code %d for bas request: %s", resp.Code, resp.Body.String())
	}
}
Exemplo n.º 6
0
func TestListNotJSON(t *testing.T) {
	h, _, mgr := makeList([]Order{})
	defer mgr.Connection().Close()

	resp, err := jsonapi.HandlerTest(jsonapi.APIHandler(h.Handle).Handler).Post("/api/list", "", ``)

	if err != nil {
		t.Fatalf("unexpected error occured when testing list: %s", err)
	}

	if resp.Code != http.StatusBadRequest {
		t.Fatalf("unexpected status code %d for bad request: %s", resp.Code, resp.Body.String())
	}
}
Exemplo n.º 7
0
func TestAddOK(t *testing.T) {
	h, token, mgr := makeAdd([]Order{})
	defer mgr.Connection().Close()

	resp, err := jsonapi.HandlerTest(jsonapi.APIHandler(h.Handle).Handler).Post(
		"/api/add",
		"",
		`{"data":{"when":1468248043,"foreign":100,"local":-100,"code":"AUD"},"token":"`+token+`"}`,
	)

	if err != nil {
		t.Fatalf("unexpected error occured when testing add: %s", err)
	}

	if resp.Code != http.StatusOK {
		t.Fatalf("unexpected error occured when testing add with status code %d: %s", resp.Code, resp.Body.String())
	}

	if resp.Body == nil {
		t.Fatal(resp)
	}

	if str := strings.TrimSpace(resp.Body.String()); str != "null" {
		t.Errorf("add: returned non-null result: '%s'", str)
	}

	qstr := `SELECT * FROM orders`
	rows := mgr.Query(Order{}, qstr)
	expect := []Order{
		Order{1468248043, 100, -100, "AUD"},
	}

	var orders []Order
	for rows.Next() {
		var o Order
		rows.Scan(&o)
		orders = append(orders, o)
	}

	if err := rows.Err(); err != nil {
		t.Fatalf("add: error reading records from db: %s", err)
	}

	msgs := validateOrders(expect, orders)
	for _, msg := range msgs {
		t.Errorf("add: %s", msg)
	}
}
Exemplo n.º 8
0
func TestAddWrongToken(t *testing.T) {
	h, _, mgr := makeAdd([]Order{})
	defer mgr.Connection().Close()

	resp, err := jsonapi.HandlerTest(jsonapi.APIHandler(h.Handle).Handler).Post(
		"/api/add",
		"",
		`{"data":{"when":1468248043,"foreign":100,"local":-100,"code":"AUD"},"token":"1234"}`,
	)

	if err != nil {
		t.Fatalf("unexpected error occured when testing add: %s", err)
	}

	if resp.Code != http.StatusForbidden {
		t.Fatalf("expect add return forbidden when wrong token, got %d: %s", resp.Code, resp.Body.String())
	}
}
Exemplo n.º 9
0
func TestListWrongToken(t *testing.T) {
	h, _, mgr := makeList([]Order{})
	defer mgr.Connection().Close()

	resp, err := jsonapi.HandlerTest(jsonapi.APIHandler(h.Handle).Handler).Post(
		"/api/list",
		"",
		`{"code":"USD","token":"1234"}`,
	)

	if err != nil {
		t.Fatalf("unexpected error occured when testing list: %s", err)
	}

	if resp.Code != http.StatusForbidden {
		t.Fatalf("unexpected status code %d for forbidden: %s", resp.Code, resp.Body.String())
	}
}
Exemplo n.º 10
0
func TestAuthOK(t *testing.T) {
	fake := FakeAuthenticator("123456")
	h := &auth{fake}

	resp, err := jsonapi.HandlerTest(jsonapi.APIHandler(h.Handle).Handler).Post("/api/auth", "", `{"pin":"123456"}`)

	if err != nil {
		t.Fatalf("unexpected error occured when testing auth: %s", err)
	}

	if resp.Code != http.StatusOK {
		t.Fatalf("auth: sent correct pin but got http status %d", resp.Code)
	}

	if resp.Body == nil {
		t.Fatal("auth: got 200 OK, but no token")
	}
}
Exemplo n.º 11
0
func TestListAllEmpty(t *testing.T) {
	h, token, mgr := makeListAll([]Order{})
	defer mgr.Connection().Close()

	resp, err := jsonapi.HandlerTest(jsonapi.APIHandler(h.Handle).Handler).Post("/api/listall", "", `{"token":"`+token+`"}`)

	if err != nil {
		t.Fatalf("unexpected error occured when testing listall: %s", err)
	}

	if resp.Code != http.StatusOK {
		t.Fatalf("unexpected error occured when testing listall with status code %d: %s", resp.Code, resp.Body.String())
	}

	if resp.Body == nil {
		t.Fatal(resp)
	}
	if str := strings.TrimSpace(resp.Body.String()); str != "[]" {
		t.Errorf("listall: not returning empty array: '%s'", str)
	}
}
Exemplo n.º 12
0
func TestAddDuplicated(t *testing.T) {
	presetData := []Order{
		Order{1468248039, 100, -100, "USD"},
	}
	h, token, mgr := makeAdd(presetData)
	defer mgr.Connection().Close()

	resp, err := jsonapi.HandlerTest(jsonapi.APIHandler(h.Handle).Handler).Post(
		"/api/add",
		"",
		`{"data":{"when":1468248039,"foreign":100,"local":-100,"code":"USD"},"token":"`+token+`"}`,
	)

	if err != nil {
		t.Fatalf("unexpected error occured when testing add: %s", err)
	}

	if resp.Code != http.StatusInternalServerError {
		t.Fatalf("unexpected status code %d for duplicated record: %s", resp.Code, resp.Body.String())
	}
}