hlfw.ca

webbing

Download patch

ref: 43222c28821e0cfd3c0bb7b8ef51b0a3b680dd2c
parent: 74303acf0bc6eb6c58771657fdc87001dd435c6e
author: halfwit <michaelmisch1985@gmail.com>
date: Tue Mar 31 04:31:21 PDT 2020

Add some form tests and helpers

--- /dev/null
+++ b/doctor/profile_test.go
@@ -1,0 +1,31 @@
+package forms
+
+import (
+	"net/url"
+	"testing"
+
+	"github.com/olmaxmedical/forms/util"
+)
+
+func TestProfile(t *testing.T) {
+	values := url.Values{}
+
+	values.Add("BTCperU", "0.1234")
+	values.Add("startDate", "2020-04-04T00:00:00")
+	values.Add("endDate", "2020-06-06T00:00:00")
+
+	if e := util.TestValues(values, profile); e != nil {
+		t.Error(e)
+	}
+
+	values.Set("BTCperU", "-1")
+	if e := util.TestValues(values, profile); e == nil {
+		t.Error("invalid BTC rate allowed")
+	}
+
+	values.Set("BTCperU", "0.1234")
+	values.Set("startDate", "1995-30-30T23:23:59")
+	if e := util.TestValues(values, profile); e == nil {
+		t.Error("invalid date allowed")
+	}
+}
--- /dev/null
+++ b/patient/offer_test.go
@@ -1,0 +1,31 @@
+package forms
+
+import (
+	"net/url"
+	"testing"
+
+	"github.com/olmaxmedical/forms/util"
+)
+
+func TestOffer(t *testing.T) {
+	values := url.Values{}
+
+	values.Add("Amount", "0.1234")
+	values.Add("startDate", "2020-04-04T00:00:00")
+	values.Add("endDate", "2020-06-06T00:00:00")
+
+	if e := util.TestValues(values, offer); e != nil {
+		t.Error(e)
+	}
+
+	values.Set("Amount", "-1")
+	if e := util.TestValues(values, offer); e == nil {
+		t.Error("invalid BTC rate allowed")
+	}
+
+	values.Set("Amount", "0.1234")
+	values.Set("startDate", "1995-30-30T23:23:59")
+	if e := util.TestValues(values, offer); e == nil {
+		t.Error("invalid date allowed")
+	}
+}
--- /dev/null
+++ b/patient/symptoms_test.go
@@ -1,0 +1,53 @@
+package forms
+
+import (
+	"net/url"
+	"testing"
+	"time"
+
+	"github.com/olmaxmedical/forms/util"
+)
+
+func TestSymptoms(t *testing.T) {
+	values := url.Values{}
+
+	values.Add("bday", "1990-01-01T01:01:01")
+	values.Add("onset", "2001-01-01T01:01:01")
+	values.Add("gender", "male")
+	values.Add("duration", "1")
+	values.Add("reason", "test")
+	values.Add("location", "test")
+	values.Add("characteristic", "test")
+	values.Add("aggreAlevi", "test")
+	for _, i := range []string{
+		"feversChills",
+		"wtGainLoss",
+		"vision",
+		"lung",
+		"heart",
+		"bowel",
+		"renal",
+		"musSkel",
+		"neuro",
+		"psych",
+	} {
+		values.Add(i, "yes")
+	}
+
+	if e := util.TestValues(values, symptoms); e != nil {
+		t.Error(e)
+	}
+
+	values.Set("bday", "1891-01-01T01:01:01")
+
+	if e := util.TestValues(values, symptoms); e == nil {
+		t.Error("forms parsing: invalid date accepted")
+	}
+
+	values.Set("bday", "1990-01-01T01:01:01")
+	values.Set("onset", time.Now().Add(time.Hour+48).String())
+
+	if e := util.TestValues(values, symptoms); e == nil {
+		t.Error("form parsing: invalid onset accepted")
+	}
+}
binary files /dev/null b/resources/certificate.pdf differ
binary files /dev/null b/resources/resume.pdf differ
--- /dev/null
+++ b/util/testing.go
@@ -1,0 +1,54 @@
+package util
+
+import (
+	"bytes"
+	"errors"
+	"io"
+	"mime/multipart"
+	"net/http"
+	"net/http/httptest"
+	"net/url"
+	"os"
+
+	"golang.org/x/text/message"
+)
+
+func BuildMultiRequest(mpw *multipart.Writer, buff *bytes.Buffer) *http.Request {
+	mpw.Close()
+
+	req := httptest.NewRequest("POST", "/", buff)
+	req.Header.Add("Content-Type", "multipart/form-data; boundary="+mpw.Boundary())
+
+	return req
+}
+
+func WriteFile(mpw *multipart.Writer, key, path string) error {
+	file, err := os.Open(path)
+	if err != nil {
+		return err
+	}
+
+	defer file.Close()
+
+	fw, err := mpw.CreateFormFile(key, path)
+	if err != nil {
+		return err
+	}
+
+	_, err = io.Copy(fw, file)
+	return err
+}
+
+func TestValues(values url.Values, fn func(*http.Request, *message.Printer) []string) error {
+	req := httptest.NewRequest("GET", "/", nil)
+
+	req.PostForm = values
+	req.Header.Set("Content-Type", "form-urlencoded")
+
+	printer := message.NewPrinter(message.MatchLanguage("en"))
+	for _, err := range fn(req, printer) {
+		return errors.New(err)
+	}
+
+	return nil
+}