Skip to content
This repository has been archived by the owner on May 19, 2020. It is now read-only.

Commit

Permalink
Merge pull request #1259 from govau/ji-skinnable-templates
Browse files Browse the repository at this point in the history
Move Go templates into `templates dir for skinning
  • Loading branch information
jcscottiii authored Oct 17, 2017
2 parents 3bbde79 + 75916a8 commit 760b10c
Show file tree
Hide file tree
Showing 17 changed files with 68 additions and 57 deletions.
2 changes: 1 addition & 1 deletion codecheck.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ go get \
golang.org/x/tools/cmd/cover


export BASE_PATH=$(cd $(dirname $0); pwd -P)
export TEST_ROOT_PATH=$(cd $(dirname $0); pwd -P)

# The value of the exit status for the script.
scriptreturn=0
Expand Down
2 changes: 1 addition & 1 deletion controllers/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func InitApp(envVars *helpers.EnvVars, env *cfenv.App) (*web.Router, *helpers.Se
}

// Cache templates
templates, err := helpers.InitTemplates(settings.BasePath)
templates, err := helpers.InitTemplates(settings.TemplatesPath)
if err != nil {
return nil, nil, err
}
Expand Down
20 changes: 10 additions & 10 deletions controllers/uaa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,16 +199,16 @@ var inviteUsersTest = []BasicProxyTest{
}

func TestInviteUsers(t *testing.T) {
for _, test := range inviteUsersTest {
// Create the external server that the proxy will send the request to.
testServer := CreateExternalServerForPrivileged(t, test)
// Construct full url for the proxy.
fullURL := fmt.Sprintf("%s%s", testServer.URL, test.RequestPath)
c := &controllers.UAAContext{SecureContext: &controllers.SecureContext{Context: &controllers.Context{}}}
response, request, router := PrepareExternalServerCall(t, c.SecureContext, testServer, fullURL, test)
router.ServeHTTP(response, request)
VerifyExternalCallResponse(t, response, &test)
testServer.Close()
for _, tt := range inviteUsersTest {
t.Run(tt.BasicSecureTest.TestName, func(t *testing.T) {
testServer := CreateExternalServerForPrivileged(t, tt)
defer testServer.Close()
fullURL := fmt.Sprintf("%s%s", testServer.URL, tt.RequestPath)
c := &controllers.UAAContext{SecureContext: &controllers.SecureContext{Context: &controllers.Context{}}}
response, request, router := PrepareExternalServerCall(t, c.SecureContext, testServer, fullURL, tt)
router.ServeHTTP(response, request)
VerifyExternalCallResponse(t, response, &tt)
})
}
}

Expand Down
10 changes: 8 additions & 2 deletions devtools/manual-setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,18 @@ Now when you're done, you'll want to stop the `testing-server` that is running i
To kill that process, run `kill %N` where "N" is the number from the line.

## Unit Testing

### Running Go unit tests
- `go test $(glide nv)`

```sh
TEST_ROOT_PATH=`pwd` go test $(glide nv)
```

### Running Javascript unit tests

Test can then be run with the command:
```

```sh
npm run test
```

Expand Down
2 changes: 0 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,6 @@ services:
image: golang:1.8.3
environment:
- GOPATH=/go
# not a GO convention but useful for running go tests.
- BASE_PATH=/go/src/github.com/18F/cg-dashboard
- DOCKER_IN_DOCKER=1
volumes:
- .:/go/src/github.com/18F/cg-dashboard
Expand Down
4 changes: 2 additions & 2 deletions helpers/env_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ var (
// For compatibility with legacy environments, if the new variables
// are not set, we fallback to this variable, and cast it to []byte.
LegacySessionKeyEnvVar = "SESSION_KEY"
// BasePathEnvVar is the path to the application root
BasePathEnvVar = "BASE_PATH"
// TemplatesPathEnvVar is the path to the templates directory.
TemplatesPathEnvVar = "TEMPLATES_PATH"
// SMTPHostEnvVar is SMTP host for UAA invites
SMTPHostEnvVar = "SMTP_HOST"
// SMTPPortEnvVar is SMTP post for UAA invites
Expand Down
6 changes: 3 additions & 3 deletions helpers/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ type Settings struct {
UaaURL string
// Log API
LogURL string
// Path to root of project.
BasePath string
// TemplatesPath is the path to the templates directory.
TemplatesPath string
// High Privileged OauthConfig
HighPrivilegedOauthConfig *clientcredentials.Config
// A flag to indicate whether profiling should be included (debug purposes).
Expand Down Expand Up @@ -111,7 +111,7 @@ func (s *Settings) InitSettings(envVars *EnvVars, env *cfenv.App) (retErr error)
}
}()

s.BasePath = envVars.String(BasePathEnvVar, "")
s.TemplatesPath = envVars.String(TemplatesPathEnvVar, "./templates")
s.AppURL = envVars.MustString(HostnameEnvVar)
s.ConsoleAPI = envVars.MustString(APIURLEnvVar)
s.LoginURL = envVars.MustString(LoginURLEnvVar)
Expand Down
5 changes: 2 additions & 3 deletions helpers/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ const (
// given the basePath of where to look.
func findTemplates(basePath string) map[string][]string {
return map[string][]string{
IndexTemplate: {filepath.Join(basePath, "static", "index.html")},
InviteEmailTemplate: {filepath.Join(basePath,
"templates", "mail", "invite.tmpl")},
IndexTemplate: {filepath.Join(basePath, "web", "index.html")},
InviteEmailTemplate: {filepath.Join(basePath, "mail", "invite.html")},
}
}

Expand Down
33 changes: 10 additions & 23 deletions helpers/templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package helpers_test
import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"testing"

Expand All @@ -12,19 +11,19 @@ import (

func TestInitTemplates(t *testing.T) {
// Valid case: correct base path to templates.
_, err := helpers.InitTemplates(os.Getenv(helpers.BasePathEnvVar))
_, err := helpers.InitTemplates(filepath.Join("testdata", "templates"))
if err != nil {
t.Errorf("Expected to find the templates. %s", err.Error())
}
// Invalid case: incorrect base path to templates.
_, err = helpers.InitTemplates("blah")
_, err = helpers.InitTemplates(filepath.Join("testdata", "non-existent-path"))
if err == nil {
t.Error("Expected not to find the templates")
}
}

func TestGetInviteEmail(t *testing.T) {
templates, err := helpers.InitTemplates(os.Getenv(helpers.BasePathEnvVar))
templates, err := helpers.InitTemplates(filepath.Join("testdata", "templates"))
if err != nil {
t.Errorf("Expected to find the templates. %s", err.Error())
}
Expand All @@ -33,27 +32,21 @@ func TestGetInviteEmail(t *testing.T) {
if err != nil {
t.Errorf("Expected no error getting the invite email. %s", err.Error())
}
inviteTpl, err := ioutil.ReadFile(filepath.Join(
os.Getenv(helpers.BasePathEnvVar),
"helpers", "testdata", "mail", "invite.html"))
inviteTpl, err := ioutil.ReadFile(filepath.Join("testdata", "templates", "mail", "invite.html"))
if err != nil {
t.Errorf("Expected no error reading the invite email. %s", err.Error())
return
}
if string(inviteTpl) != string(body.Bytes()) {
t.Error("Expected invite e-mail template does not match generated invite e-mail template.")
// Helpful for generating the new invite data.
ioutil.WriteFile(filepath.Join(
os.Getenv(helpers.BasePathEnvVar),
"helpers", "testdata", "mail", "invite.html.returned"), body.Bytes(), 0444)
t.Logf("writing expected file to %s", filepath.Join(
os.Getenv(helpers.BasePathEnvVar),
"helpers", "testdata", "mail", "invite.html.returned"))
ioutil.WriteFile(filepath.Join("testdata", "templates", "mail", "invite.html.returned"), body.Bytes(), 0444)
t.Logf("writing expected file to %s", filepath.Join("testdata", "templates", "mail", "invite.html.returned"))
}
}

func TestGetIndex(t *testing.T) {
templates, err := helpers.InitTemplates(os.Getenv(helpers.BasePathEnvVar))
templates, err := helpers.InitTemplates(filepath.Join("testdata", "templates"))
if err != nil {
t.Errorf("Expected to find the templates. %s", err.Error())
}
Expand All @@ -63,21 +56,15 @@ func TestGetIndex(t *testing.T) {
if err != nil {
t.Errorf("Expected no error getting the index html. %s", err.Error())
}
inviteTpl, err := ioutil.ReadFile(filepath.Join(
os.Getenv(helpers.BasePathEnvVar),
"helpers", "testdata", "index.html"))
inviteTpl, err := ioutil.ReadFile(filepath.Join("testdata", "templates", "web", "index.html"))
if err != nil {
t.Errorf("Expected no error reading the index.html. %s", err.Error())
return
}
if string(inviteTpl) != string(body.Bytes()) {
t.Error("Expected index.html template does not match generated index.html template.")
// Helpful for generating the new invite data.
ioutil.WriteFile(filepath.Join(
os.Getenv(helpers.BasePathEnvVar),
"helpers", "testdata", "index.html.returned"), body.Bytes(), 0444)
t.Logf("writing expected file to %s", filepath.Join(
os.Getenv(helpers.BasePathEnvVar),
"helpers", "testdata", "index.html.returned"))
ioutil.WriteFile(filepath.Join("testdata", "templates", "web", "index.html.returned"), body.Bytes(), 0444)
t.Logf("writing expected file to %s", filepath.Join("testdata", "templates", "web", "index.html.returned"))
}
}
File renamed without changes.
8 changes: 6 additions & 2 deletions helpers/testhelpers/testhelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -122,7 +123,10 @@ func CreateRouterWithMockSession(sessionData map[string]interface{}, envVars map
// Override the session store.
settings.Sessions = store

templates, _ := helpers.InitTemplates(settings.BasePath)
templates, err := helpers.InitTemplates(settings.TemplatesPath)
if err != nil {
log.Fatalf("failed to init templates: %v", err)
}

// Create the router.
mockMailer := new(mocks.Mailer)
Expand Down Expand Up @@ -250,7 +254,7 @@ func GetMockCompleteEnvVars() map[string]string {
helpers.SessionEncryptionEnvVar: "00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff",
helpers.SessionAuthenticationEnvVar: "00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff",
helpers.CSRFKeyEnvVar: "00112233445566778899aabbccddeeff",
helpers.BasePathEnvVar: os.Getenv(helpers.BasePathEnvVar),
helpers.TemplatesPathEnvVar: filepath.Join(os.Getenv("TEST_ROOT_PATH"), "templates"),
helpers.SMTPFromEnvVar: "cloud@cloud.gov",
helpers.SMTPHostEnvVar: "localhost",
helpers.SecureCookiesEnvVar: "1",
Expand Down
7 changes: 1 addition & 6 deletions mailer/mailer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package mailer

import (
"bytes"
"os"
"strings"
"testing"

Expand All @@ -14,7 +13,6 @@ func TestSendEmail(t *testing.T) {
hostname, smtpPort, apiPort, cleanup := CreateTestMailCatcher(t)
// Test InitSMTPMailer with valid path for templates.
settings := helpers.Settings{
BasePath: os.Getenv(helpers.BasePathEnvVar),
SMTPHost: hostname,
SMTPPort: smtpPort,
SMTPFrom: "test@dashboard.com",
Expand Down Expand Up @@ -57,10 +55,7 @@ func TestSendEmail(t *testing.T) {
}

func TestInitSMTPMailer(t *testing.T) {
// Test InitSMTPMailer with valid path for templates.
settings := helpers.Settings{
BasePath: os.Getenv(helpers.BasePathEnvVar),
}
settings := helpers.Settings{}
mailer, err := InitSMTPMailer(settings)
if mailer == nil {
t.Error("Expected non nil mailer.")
Expand Down
22 changes: 21 additions & 1 deletion static_src/test/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,32 @@ export function start(...args) {
]);

// serve static assets
server.route({
method: 'get',
path: '/assets/{p*}',
handler: {
directory: {
path: 'static/assets'
}
}
});

server.route({
method: 'get',
path: '/skins/{p*}',
handler: {
directory: {
path: 'static/skins'
}
}
});

server.route({
method: 'get',
path: '/{p*}',
handler: {
directory: {
path: 'static'
path: 'templates/web'
}
}
});
Expand Down
File renamed without changes.
File renamed without changes.
4 changes: 3 additions & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ const config = {
modules: ['node_modules'],
// Required for some module configs which use these fields
// See https://github.com/flatiron/director/issues/349
mainFields: ['browserify', 'browser', 'module', 'main']
mainFields: ['browserify', 'browser', 'module', 'main'],

symlinks: false
},

plugins: [
Expand Down

0 comments on commit 760b10c

Please sign in to comment.