Skip to content

Commit

Permalink
Render domain status to HTML
Browse files Browse the repository at this point in the history
  • Loading branch information
vbrown608 committed Jan 23, 2019
1 parent 3b9d34d commit cdd32e6
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 23 deletions.
19 changes: 16 additions & 3 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,11 @@ func (api API) Queue(r *http.Request) APIResponse {
// 2. Create token for domain
token, err := api.Database.PutToken(domain)
if err != nil {
return APIResponse{StatusCode: http.StatusInternalServerError, Message: err.Error()}
return APIResponse{
StatusCode: http.StatusInternalServerError,
Message: err.Error(),
TemplatePath: "views/domain.html.tmpl",
}
}

// 3. Send email
Expand All @@ -286,14 +290,22 @@ func (api API) Queue(r *http.Request) APIResponse {
return APIResponse{StatusCode: http.StatusInternalServerError,
Message: "Unable to send validation e-mail"}
}
return APIResponse{StatusCode: http.StatusOK, Response: domainData}
return APIResponse{
StatusCode: http.StatusOK,
Response: domainData,
TemplatePath: "views/domain.html.tmpl",
}
// GET: Retrieve domain status from queue
} else if r.Method == http.MethodGet {
status, err := api.Database.GetDomain(domain)
if err != nil {
return APIResponse{StatusCode: http.StatusNotFound, Message: err.Error()}
}
return APIResponse{StatusCode: http.StatusOK, Response: status}
return APIResponse{
StatusCode: http.StatusOK,
Response: status,
TemplatePath: "views/domain.html.tmpl",
}
} else {
return APIResponse{StatusCode: http.StatusMethodNotAllowed,
Message: "/api/queue only accepts POST and GET requests"}
Expand Down Expand Up @@ -382,6 +394,7 @@ func writeHTML(w http.ResponseWriter, apiResponse APIResponse) {
}
tmpl, err := template.ParseFiles(apiResponse.TemplatePath)
if err != nil {
log.Fatal(err)
http.Error(w, "Internal error: could not parse template.", http.StatusInternalServerError)
return
}
Expand Down
22 changes: 22 additions & 0 deletions api_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package main

import (
"io/ioutil"
"net/http"
"net/url"
"strings"
"testing"

"github.com/EFForg/starttls-backend/checker"
Expand Down Expand Up @@ -40,3 +44,21 @@ func TestPolicyCheckWithQueuedDomain(t *testing.T) {
t.Errorf("Check should have warned.")
}
}

func testHTMLPost(path string, data url.Values, t *testing.T) ([]byte, int) {
req, err := http.NewRequest("POST", server.URL+path, strings.NewReader(data.Encode()))
if err != nil {
t.Fatal(err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("accept", "text/html")
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
body, _ := ioutil.ReadAll(resp.Body)
if !strings.Contains(strings.ToLower(string(body)), "<html") {
t.Errorf("Response should be HTML, got %s", string(body))
}
return body, resp.StatusCode
}
13 changes: 13 additions & 0 deletions queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"net/http"
"net/url"
"strings"
"testing"

"github.com/EFForg/starttls-backend/models"
Expand All @@ -23,6 +24,18 @@ func validQueueData(scan bool) url.Values {
return data
}

func TestQueueHTML(t *testing.T) {
defer teardown()

body, status := testHTMLPost("/api/queue", validQueueData(true), t)
if status != http.StatusOK {
t.Errorf("HTML POST to api/queue failed with error %d", status)
}
if !strings.Contains(string(body), "Thank you for submitting your domain") {
t.Errorf("Response should describe domain status, got %s", string(body))
}
}

func TestGetDomainHidesEmail(t *testing.T) {
defer teardown()

Expand Down
23 changes: 3 additions & 20 deletions scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,15 @@ import (
"github.com/EFForg/starttls-backend/models"
)

func htmlPost(path string, data url.Values) (*http.Response, error) {
req, err := http.NewRequest("POST", server.URL+path, strings.NewReader(data.Encode()))
if err != nil {
return &http.Response{}, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("accept", "text/html")
return http.DefaultClient.Do(req)
}

func TestScanHTML(t *testing.T) {
defer teardown()

// Request a scan!
data := url.Values{}
data.Set("domain", "eff.org")
resp, err := htmlPost("/api/scan", data)
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != http.StatusOK {
t.Errorf("HTML POST to api/scan failed with error %d", resp.StatusCode)
}
body, _ := ioutil.ReadAll(resp.Body)
if !strings.Contains(strings.ToLower(string(body)), "<html") {
t.Errorf("Response should contain scan domain, got %s", string(body))
body, status := testHTMLPost("/api/scan", data, t)
if status != http.StatusOK {
t.Errorf("HTML POST to api/scan failed with error %d", status)
}
if !strings.Contains(string(body), "eff.org") {
t.Errorf("Response should contain scan domain, got %s", string(body))
Expand Down
19 changes: 19 additions & 0 deletions views/domain.html.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<html>
<body>
{{ if or (eq .Response.State "unknown") (eq .Response.State "") }}
Something went wrong - your domain was not submitted to the STARTTLS policy list.
{{ end }}
{{ if eq .Response.State "unvalidated" }}
Thank you for submitting your domain. Please check postmaster@{{ .Response.Name }} to validate that you control the domain.
{{ end }}
{{ if eq .Response.State "queued" }}
Thank you for submitting your domain. Your domain is queued to be reviewed and added to the STARTTLS policy list.
{{ end }}
{{ if eq .Response.State "failed" }}
Your domain does not qualify for addition to the STARTTLS policy list. Please visit {{ .BaseURL }} to review your domain's status and correct any issues.
{{ end }}
{{ if eq .Response.State "added" }}
Your domain is already on the STARTTLS policy list. Thanks for helping to make e-mail more secure.
{{ end }}
</body>
</html>

0 comments on commit cdd32e6

Please sign in to comment.