/
render_test.go
113 lines (100 loc) · 4.61 KB
/
render_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package render_test
import (
"errors"
"io"
"net/http"
"net/http/httptest"
"testing"
render "github.com/ONSdigital/dp-renderer/v2"
"github.com/ONSdigital/dp-renderer/v2/model"
. "github.com/smartystreets/goconvey/convey"
)
func TestRenderPageMethod(t *testing.T) {
Convey("Given the renderer is instantiated with a render client", t, func() {
mockClient := newMockRenderingClient([]string{"test-article-page", "test-homepage", "error/401", "error/404", "error/500"})
renderer := render.New(mockClient, "path/to/assets", "site-domain")
w := httptest.NewRecorder()
Convey("When the renderer's Page method is called with a valid template name and page model", func() {
mockPage := renderer.NewBasePageModel()
renderer.BuildPage(w, mockPage, "test-homepage")
Convey("Then the render client's HTML method should be called, and the page model should have assets path and site domain injected", func() {
So(len(mockClient.ValidBuildHTMLMethodCalls), ShouldEqual, 1)
So(mockClient.ValidSetErrorMethodCalls, ShouldEqual, 0)
So(mockPage.PatternLibraryAssetsPath, ShouldEqual, "path/to/assets")
So(mockPage.SiteDomain, ShouldEqual, "site-domain")
})
})
Convey("When the renderer's Page method is called with an invalid template name", func() {
mockPage := renderer.NewBasePageModel()
renderer.BuildPage(w, mockPage, "")
Convey("Then the render client should set an error", func() {
So(mockClient.ValidBuildHTMLMethodCalls, ShouldBeEmpty)
So(mockClient.ValidSetErrorMethodCalls, ShouldEqual, 1)
})
})
Convey("When the renderer's build error page method is called", func() {
mockPage := renderer.NewBasePageModel()
Convey("Then the render client should set pageModel and call build HTML func", func() {
Convey("for 401", func() {
renderer.BuildErrorPage(w, mockPage, http.StatusUnauthorized)
expectedPageModel := mockClient.ValidBuildHTMLMethodCalls[0].PageModel.(model.Page)
So(mockClient.ValidBuildHTMLMethodCalls, ShouldHaveLength, 1)
So(expectedPageModel.Error.Title, ShouldEqual, "Access denied")
So(expectedPageModel.Error.ErrorCode, ShouldEqual, http.StatusUnauthorized)
So(expectedPageModel.Enable500ErrorPageStyling, ShouldBeFalse)
So(mockClient.ValidBuildHTMLMethodCalls[0].TemplateName, ShouldEqual, "error/401")
So(mockClient.ValidSetErrorMethodCalls, ShouldEqual, 0)
})
Convey("for 404", func() {
renderer.BuildErrorPage(w, mockPage, http.StatusNotFound)
expectedPageModel := mockClient.ValidBuildHTMLMethodCalls[0].PageModel.(model.Page)
So(mockClient.ValidBuildHTMLMethodCalls, ShouldHaveLength, 1)
So(expectedPageModel.Error.Title, ShouldEqual, "Page not found")
So(expectedPageModel.Error.ErrorCode, ShouldEqual, http.StatusNotFound)
So(expectedPageModel.Enable500ErrorPageStyling, ShouldBeFalse)
So(mockClient.ValidBuildHTMLMethodCalls[0].TemplateName, ShouldEqual, "error/404")
So(mockClient.ValidSetErrorMethodCalls, ShouldEqual, 0)
})
Convey("for 500", func() {
renderer.BuildErrorPage(w, mockPage, http.StatusInternalServerError)
expectedPageModel := mockClient.ValidBuildHTMLMethodCalls[0].PageModel.(model.Page)
So(mockClient.ValidBuildHTMLMethodCalls, ShouldHaveLength, 1)
So(expectedPageModel.Error.Title, ShouldEqual, "Sorry, there's a problem with the service")
So(expectedPageModel.Error.ErrorCode, ShouldEqual, http.StatusInternalServerError)
So(expectedPageModel.Enable500ErrorPageStyling, ShouldBeTrue)
So(mockClient.ValidBuildHTMLMethodCalls[0].TemplateName, ShouldEqual, "error/500")
So(mockClient.ValidSetErrorMethodCalls, ShouldEqual, 0)
})
})
})
})
}
type mockRenderClient struct {
TemplateNames []string
ValidBuildHTMLMethodCalls []ValidBuildHTMLMethod
ValidSetErrorMethodCalls int
}
type ValidBuildHTMLMethod struct {
Status int
TemplateName string
PageModel interface{}
}
func newMockRenderingClient(templateNames []string) *mockRenderClient {
return &mockRenderClient{
TemplateNames: templateNames,
}
}
func (m *mockRenderClient) BuildHTML(w io.Writer, status int, templateName string, pageModel interface{}) error {
for _, value := range m.TemplateNames {
if value == templateName {
call := ValidBuildHTMLMethod{Status: status, TemplateName: templateName, PageModel: pageModel}
m.ValidBuildHTMLMethodCalls = append(m.ValidBuildHTMLMethodCalls, call)
return nil
}
}
return errors.New("Failed to build page")
}
func (m *mockRenderClient) SetError(w io.Writer, status int, errorModel model.ErrorResponse) error {
m.ValidSetErrorMethodCalls++
return errors.New("An error occurred when building the page")
}