ref: 27a47c20e19cafd10b8a09fcd4f0177adf5312cd
parent: 577eada94e73351cb47ef146ce7ae5e37e98ad2d
parent: 43222c28821e0cfd3c0bb7b8ef51b0a3b680dd2c
author: Michael Misch <michaelmisch1985@gmail.com>
date: Tue Mar 31 04:35:07 PDT 2020
Merge pull request #5 from olmaxmedical/testing Testing
--- a/doctor/application.go
+++ b/doctor/application.go
@@ -23,9 +23,10 @@
func application(r *http.Request, p *message.Printer) []string {
var errors []string
+
data, err := forms.Parse(r)
if err != nil {
- errors = append(errors, "Internal server error")
+ errors = append(errors, fmt.Sprintf("Internal server error %v", err))
return errors
}
val := data.Validator()
--- /dev/null
+++ b/doctor/application_test.go
@@ -1,0 +1,125 @@
+package forms
+
+import (
+ "bufio"
+ "net/http"
+ "strings"
+ "testing"
+)
+
+// This isn't quite right yet, we'll get a working test soon.
+var req = `POST /doctor/application.html HTTP/1.1
+Host: localhost
+User-Agent: Mozilla/5.0
+Accept: */*
+Connection: keep-alive
+Cache-Control: no-cache
+Content-Length: 2028
+Content-Type: multipart/form-data; boundary=--------------------
+
+--------------------------4ee5f9ad0b2d7899
+Content-Disposition: form-data; name="Specialty"
+
+bariatric
+--------------------------4ee5f9ad0b2d7899
+Content-Disposition: form-data; name="country"
+
+Albania
+--------------------------4ee5f9ad0b2d7899
+Content-Disposition: form-data; name="email"
+
+mee%40foo.ca
+--------------------------4ee5f9ad0b2d7899
+Content-Disposition: form-data; name="gender"
+
+male
+--------------------------4ee5f9ad0b2d7899
+Content-Disposition: form-data; name="name"
+
+mememe
+--------------------------4ee5f9ad0b2d7899
+Content-Disposition: form-data; name="pagetitle"
+
+Application+for+doctor
+--------------------------4ee5f9ad0b2d7899
+Content-Disposition: form-data; name="q1"
+
+No
+--------------------------4ee5f9ad0b2d7899
+Content-Disposition: form-data; name="q10"
+
+No
+--------------------------4ee5f9ad0b2d7899
+Content-Disposition: form-data; name="q11"
+
+No
+--------------------------4ee5f9ad0b2d7899
+Content-Disposition: form-data; name="q2"
+
+No
+--------------------------4ee5f9ad0b2d7899
+Content-Disposition: form-data; name="q3"
+
+No
+--------------------------4ee5f9ad0b2d7899
+Content-Disposition: form-data; name="q4"
+
+No
+--------------------------4ee5f9ad0b2d7899
+Content-Disposition: form-data; name="q5"
+
+No
+--------------------------4ee5f9ad0b2d7899
+Content-Disposition: form-data; name="q6"
+
+No
+--------------------------4ee5f9ad0b2d7899
+Content-Disposition: form-data; name="q7"
+
+No
+--------------------------4ee5f9ad0b2d7899
+Content-Disposition: form-data; name="q8"
+
+No
+--------------------------4ee5f9ad0b2d7899
+Content-Disposition: form-data; name="q9"
+
+No
+--------------------------4ee5f9ad0b2d7899
+Content-Disposition: form-data; name="sendto"
+
+olmaxmedical%40gmail.com
+--------------------------4ee5f9ad0b2d7899
+Content-Disposition: form-data; name="cv"
+
+resume.pdf
+--------------------------4ee5f9ad0b2d7899
+Content-Disposition: form-data; name="form"
+
+resume.pdf
+--------------------------4ee5f9ad0b2d7899`
+
+func TestApplication(t *testing.T) {
+ // Build a POST request with a map of entries
+ rd := bufio.NewReader(strings.NewReader(req))
+
+ request, err := http.ReadRequest(rd)
+ if err != nil {
+ t.Errorf("test design error: bad request: %v", err)
+ return
+ }
+
+ if e := request.ParseMultipartForm(request.ContentLength); e != nil {
+ t.Error(e)
+ }
+ /*
+ printer := message.NewPrinter(message.MatchLanguage("en"))
+
+ resp := application(request, printer)
+ if len(resp) > 0 {
+ for _, err := range resp {
+ t.Errorf("%s", err)
+ }
+ }
+ */
+}
--- /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")
+ }
+}
--- a/go.mod
+++ b/go.mod
@@ -4,12 +4,7 @@
require (
github.com/albrow/forms v0.3.3
- github.com/olmaxmedical/database v0.0.1 // indirect
- github.com/olmaxmedical/email v0.0.1 // indirect
github.com/olmaxmedical/plugins v0.0.1
github.com/olmaxmedical/router v0.0.1
- github.com/pariz/gountries v0.0.0-20191029140926-233bc78cf5b5 // indirect
- github.com/stretchr/testify v1.5.1 // indirect
golang.org/x/text v0.3.2
- gopkg.in/yaml.v2 v2.2.8 // indirect
)
--- a/go.sum
+++ b/go.sum
@@ -9,10 +9,10 @@
github.com/olmaxmedical/database v0.0.1/go.mod h1:/5Tl6/p0jpvLpj4GaoFki3wRG/3b+ipNNhM5Dyi6Zf8=
github.com/olmaxmedical/email v0.0.1 h1:bhOERmPiUmFJqC133s+FFXucSI3dNnfDKsboDYFEbkc=
github.com/olmaxmedical/email v0.0.1/go.mod h1:bz6en9uc6h9fyu3MW2jTwYW19ZclQ22JkcIxsl3/epc=
-github.com/olmaxmedical/plugins v0.0.1 h1:KT6yy6faKLlDkGDx/RokGm+rsAxzKAzsFtb2zjaWUd8=
-github.com/olmaxmedical/plugins v0.0.1/go.mod h1:XIPHYhbFmjgxwo5ySRHXDK3Ar3Y3vcBW1k//nwMNncA=
-github.com/olmaxmedical/router v0.0.1 h1:0oDsfSoW+lP8g3yzql5QLAQgV4cTy4s+gZH7aJNJex8=
-github.com/olmaxmedical/router v0.0.1/go.mod h1:AM2lewexrBtUWFURwB9exKGvdHRGbKMEb1GL3iHX4js=
+github.com/olmaxmedical/plugins v0.0.1 h1:fON9AAfH635gLKNbytrUdRoRCjhI0ZPP7VFzCVZDVWA=
+github.com/olmaxmedical/plugins v0.0.1/go.mod h1:bHkYv5oh6bk5y1jCZDO9Bk5IyYMPWgZDjMrEifxVgbU=
+github.com/olmaxmedical/router v0.0.1 h1:x3fZ9u00xwKxLvPj8Au0QhIxpmxRv+q2lVMdBmyRQjM=
+github.com/olmaxmedical/router v0.0.1/go.mod h1:28e377pByZCQMBAdERDBhX4wYeTsJgc6U4yOEiz7MsA=
github.com/olmaxmedical/session v0.0.1 h1:2xdSjpEg89+ClRFLPp/gR3lNxl4JjPAUd2Hsds++FFs=
github.com/olmaxmedical/session v0.0.1/go.mod h1:XOVyHL+cKa5t2fLDIJtFxwEzJOa3r1hUkwlL4aybdqA=
github.com/pariz/gountries v0.0.0-20191029140926-233bc78cf5b5 h1:842t0ixg/A4my8/Q3oDNdHIsKYIx02NDlWVEhaiBToo=
--- /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
+}