dnote/pkg/server/web/handlers_test.go
Sung Won Cho 0bcd06600e
Fix direct access to note pages (#324)
* Initialize database

* Document change

* Test regression
2019-11-20 14:38:39 +08:00

92 lines
2.1 KiB
Go

package web
import (
"fmt"
"net/http"
"testing"
"github.com/dnote/dnote/pkg/assert"
"github.com/jinzhu/gorm"
"github.com/pkg/errors"
)
func TestInit(t *testing.T) {
mockIndexHTML := []byte("<html></html>")
mockRobotsTxt := []byte("Allow: *")
mockServiceWorkerJs := []byte("function() {}")
mockStaticFileSystem := http.Dir(".")
testCases := []struct {
ctx Context
expectedErr error
}{
{
ctx: Context{
DB: &gorm.DB{},
IndexHTML: mockIndexHTML,
RobotsTxt: mockRobotsTxt,
ServiceWorkerJs: mockServiceWorkerJs,
StaticFileSystem: mockStaticFileSystem,
},
expectedErr: nil,
},
{
ctx: Context{
DB: nil,
IndexHTML: mockIndexHTML,
RobotsTxt: mockRobotsTxt,
ServiceWorkerJs: mockServiceWorkerJs,
StaticFileSystem: mockStaticFileSystem,
},
expectedErr: ErrEmptyDB,
},
{
ctx: Context{
DB: &gorm.DB{},
IndexHTML: nil,
RobotsTxt: mockRobotsTxt,
ServiceWorkerJs: mockServiceWorkerJs,
StaticFileSystem: mockStaticFileSystem,
},
expectedErr: ErrEmptyIndexHTML,
},
{
ctx: Context{
DB: &gorm.DB{},
IndexHTML: mockIndexHTML,
RobotsTxt: nil,
ServiceWorkerJs: mockServiceWorkerJs,
StaticFileSystem: mockStaticFileSystem,
},
expectedErr: ErrEmptyRobotsTxt,
},
{
ctx: Context{
DB: &gorm.DB{},
IndexHTML: mockIndexHTML,
RobotsTxt: mockRobotsTxt,
ServiceWorkerJs: nil,
StaticFileSystem: mockStaticFileSystem,
},
expectedErr: ErrEmptyServiceWorkerJS,
},
{
ctx: Context{
DB: &gorm.DB{},
IndexHTML: mockIndexHTML,
RobotsTxt: mockRobotsTxt,
ServiceWorkerJs: mockServiceWorkerJs,
StaticFileSystem: nil,
},
expectedErr: ErrEmptyStaticFileSystem,
},
}
for idx, tc := range testCases {
t.Run(fmt.Sprintf("test case %d", idx), func(t *testing.T) {
_, err := Init(tc.ctx)
assert.Equal(t, errors.Cause(err), tc.expectedErr, "error mismatch")
})
}
}