1
0
mirror of https://github.com/aclindsa/moneygo.git synced 2024-10-31 16:00:05 -04:00

Add negative security handler tests

This commit is contained in:
Aaron Lindsay 2017-10-05 21:08:17 -04:00
parent a6e2b33dd0
commit 2fe25cbb77
2 changed files with 58 additions and 2 deletions

View File

@ -4,6 +4,7 @@ import (
"encoding/json"
"log"
"net/http"
"strings"
)
type Error struct {
@ -11,6 +12,16 @@ type Error struct {
ErrorString string
}
func (e *Error) Read(json_str string) error {
dec := json.NewDecoder(strings.NewReader(json_str))
return dec.Decode(e)
}
func (e *Error) Write(w http.ResponseWriter) error {
enc := json.NewEncoder(w)
return enc.Encode(e)
}
var error_codes = map[int]string{
1: "Not Signed In",
2: "Unauthorized Access",
@ -29,8 +40,7 @@ func WriteError(w http.ResponseWriter, error_code int) {
}
e := Error{error_code, msg}
enc := json.NewEncoder(w)
err := enc.Encode(e)
err := e.Write(w)
if err != nil {
log.Fatal(err)
}

View File

@ -108,3 +108,49 @@ func TestSecurityTemplateLimit(t *testing.T) {
t.Fatalf("Requested only 5 securities, received %d\n", len(*sl.Securities))
}
}
func TestSecurityTemplateInvalidType(t *testing.T) {
var e handlers.Error
response, err := http.Get(server.URL + "/securitytemplate/?search=e&type=blah")
if err != nil {
t.Fatal(err)
}
body, err := ioutil.ReadAll(response.Body)
response.Body.Close()
if err != nil {
t.Fatal(err)
}
err = (&e).Read(string(body))
if err != nil {
t.Fatal(err)
}
if e.ErrorId != 3 {
t.Fatal("Expected ErrorId 3, Invalid Request")
}
}
func TestSecurityTemplateInvalidLimit(t *testing.T) {
var e handlers.Error
response, err := http.Get(server.URL + "/securitytemplate/?search=e&type=Currency&limit=foo")
if err != nil {
t.Fatal(err)
}
body, err := ioutil.ReadAll(response.Body)
response.Body.Close()
if err != nil {
t.Fatal(err)
}
err = (&e).Read(string(body))
if err != nil {
t.Fatal(err)
}
if e.ErrorId != 3 {
t.Fatal("Expected ErrorId 3, Invalid Request")
}
}