initial project release

This commit is contained in:
stremovsky
2019-12-09 00:25:27 +02:00
parent 83cac10e5e
commit 15a00fd649
45 changed files with 5734 additions and 0 deletions

72
src/utils_test.go Normal file
View File

@@ -0,0 +1,72 @@
package main
import (
"net/http/httptest"
"strings"
"testing"
uuid "github.com/hashicorp/go-uuid"
)
func Test_UUID(t *testing.T) {
for id := 1; id < 11; id++ {
recordUUID, err := uuid.GenerateUUID()
t.Logf("Checking[%d]: %s\n", id, recordUUID)
if err != nil {
t.Fatalf("Failed to generate UUID %s: %s ", recordUUID, err)
} else if isValidUUID(recordUUID) == false {
t.Fatalf("Failed to validate UUID: %s ", recordUUID)
}
}
}
func Test_AppNames(t *testing.T) {
goodApps := []string{"penn", "teller", "a123"}
for _, value := range goodApps {
if isValidApp(value) == false {
t.Fatalf("Failed to validate good app name: %s ", value)
}
}
badApps := []string{"P1", "1as", "_a", "a_a", "a.a", "a a"}
for _, value := range badApps {
if isValidApp(value) == true {
t.Fatalf("Failed to validate bad app name: %s ", value)
}
}
}
func Test_getJSONPost(t *testing.T) {
goodJsons := []string{
`{"login":"abc","name": "tom", "pass": "mylittlepony", "admin": true}`,
`{"login":"1234","name": "tom", "pass": "mylittlepony", "admin": true}`,
}
for _, value := range goodJsons {
request := httptest.NewRequest("POST", "/user", strings.NewReader(value))
request.Header.Set("Content-Type", "application/json")
result, err := getJSONPost(request, "IL")
if err != nil {
t.Fatalf("Failed to parse json: %s, err: %s\n", value, err)
}
if len(result.loginIdx) == 0 {
t.Fatalf("Failed to parse login index from json: %s ", value)
}
}
badJsons := []string{
`{"login":true,"name": "tom", "pass": "mylittlepony", "admin": true}`,
`{"login":1,"name": "tom", "pass": "mylittlepony", "admin": true}`,
`{"login":null,"name": "tom", "pass": "mylittlepony", "admin": true}`,
}
for _, value := range badJsons {
request := httptest.NewRequest("POST", "/user", strings.NewReader(value))
request.Header.Set("Content-Type", "application/json")
result, err := getJSONPost(request, "IL")
if err != nil {
t.Fatalf("Failed to parse json: %s, err: %s\n", value, err)
}
if len(result.loginIdx) != 0 {
t.Fatalf("Failed to parse login index from json: %s ", value)
}
}
}