Created
August 10, 2014 21:54
-
-
Save levicook/1de85c437a684e258ce4 to your computer and use it in GitHub Desktop.
Excerpt of a testing strategy for Go http handlers.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package repos | |
| import ( | |
| "fabric-server/models" | |
| "labix.org/v2/mgo" | |
| "labix.org/v2/mgo/bson" | |
| ) | |
| type CampusRepo interface { | |
| All() models.Campuses | |
| AllById(...models.CampusId) models.Campuses | |
| Count() int | |
| Create(*models.Campus) models.Errors | |
| } | |
| func NewCampusRepo() CampusRepo { | |
| return newCampusRepo(session.DB("")) | |
| } | |
| type campusRepo struct{ c *mgo.Collection } | |
| func newCampusRepo(db *mgo.Database) campusRepo { | |
| c := db.C("campuses") | |
| return campusRepo{c: c} | |
| } | |
| func (r campusRepo) All() (c models.Campuses) { | |
| panicIf(r.c.Find(nil).All(&c)) | |
| return | |
| } | |
| func (r campusRepo) AllById(ids ...models.CampusId) (c models.Campuses) { | |
| panicIf(r.c.Find(m{"_id": toOids(ids)}).All(&c)) | |
| return | |
| } | |
| func (r campusRepo) Count() int { | |
| c, e := r.c.Count() | |
| panicIf(e) | |
| return c | |
| } | |
| func (r campusRepo) Create(o *models.Campus) models.Errors { | |
| if errors := o.Errors(); errors.Present() { | |
| return errors | |
| } | |
| oid := bson.NewObjectId() | |
| o.Id = models.CampusId(oid.Hex()) | |
| doc := toDoc(o) | |
| doc["_id"] = oid | |
| doc["cat"] = oid.Time() | |
| doc["uat"] = oid.Time() | |
| panicIf(r.c.Insert(doc)) | |
| return noErrors | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package repos | |
| import "fabric-server/models" | |
| // make sure FakeCampusRepo implements CampusRepo | |
| var _ CampusRepo = (*fakeCampusRepo)(nil) | |
| func NewFakeCampusRepo() *fakeCampusRepo { | |
| return &fakeCampusRepo{ | |
| AllStub: func() models.Campuses { panic("stub called!") }, | |
| AllByIdStub: func(...models.CampusId) models.Campuses { panic("stub called!") }, | |
| CountStub: func() int { panic("stub called!") }, | |
| CreateStub: func(*models.Campus) models.Errors { panic("stub called!") }, | |
| } | |
| } | |
| type fakeCampusRepo struct { | |
| AllStub func() models.Campuses | |
| AllByIdStub func(...models.CampusId) models.Campuses | |
| CreateStub func(*models.Campus) models.Errors | |
| CountStub func() int | |
| } | |
| func (r fakeCampusRepo) All() models.Campuses { | |
| return r.AllStub() | |
| } | |
| func (r fakeCampusRepo) AllById(ids ...models.CampusId) models.Campuses { | |
| return r.AllByIdStub(ids...) | |
| } | |
| func (r fakeCampusRepo) Count() int { | |
| return r.CountStub() | |
| } | |
| func (r fakeCampusRepo) Create(m *models.Campus) models.Errors { | |
| return r.CreateStub(m) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package campuses | |
| import ( | |
| "fabric-server/models" | |
| "fabric-server/models/roles" | |
| "fabric-server/repos" | |
| "fabric-server/web/sessions" | |
| "fabric-server/web/utils/pages" | |
| "fabric-server/web/utils/send" | |
| "net/http" | |
| ) | |
| func Index(w http.ResponseWriter, r *http.Request) { | |
| indexHtml(w, r) | |
| } | |
| func indexHtml(w http.ResponseWriter, r *http.Request) { | |
| repo := newCampusRepo() | |
| sess := newSession(r) | |
| if sess.Anonymous() { | |
| send.ThroughAdminLogin(w, r) | |
| return | |
| } | |
| if !sess.HasRole(roles.SystemAdmin) { | |
| send.Forbidden(w) | |
| return | |
| } | |
| page := pages.New(`pages/admin/campuses/index`, r.Header) | |
| page.WritePool(indexPoolFor(sess, repo)) | |
| send.Page(w, page) | |
| } | |
| func indexPoolFor(s sessions.Session, repo repos.CampusRepo) indexPool { | |
| return indexPool{ | |
| FirstPerson: s.FirstPerson(), | |
| Campuses: repo.All(), | |
| } | |
| } | |
| type indexPool struct { | |
| FirstPerson models.FirstPerson `json:"firstPerson"` | |
| Campuses models.Campuses `json:"campuses"` | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package campuses | |
| import ( | |
| "fabric-server/models" | |
| "fabric-server/models/roles" | |
| "fabric-server/repos" | |
| "fabric-server/web/sessions" | |
| "fabric-server/web/test_helpers" | |
| "fabric-server/web/utils/status" | |
| "net/http" | |
| "net/http/httptest" | |
| "testing" | |
| "github.com/stretchr/testify/assert" | |
| ) | |
| func Test_Index_SeeOther(t *testing.T) { | |
| defer setDefaults() | |
| newSession = func(*http.Request) sessions.Session { | |
| sess := sessions.NewFakeSession() | |
| sess.AnonymousStub = func() bool { return true } | |
| return sess | |
| } | |
| newCampusRepo = func() repos.CampusRepo { | |
| return repos.NewFakeCampusRepo() | |
| } | |
| w := httptest.NewRecorder() | |
| r := test_helpers.NewRequest("GET", "/admin/campuses") | |
| Index(w, r) | |
| assert.Equal(t, w.Code, status.SeeOther) | |
| assert.Equal(t, w.Header().Get("Location"), "/admin/login?rp=%2Fadmin%2Fcampuses") | |
| } | |
| func Test_Index_Forbidden(t *testing.T) { | |
| defer setDefaults() | |
| newSession = func(*http.Request) sessions.Session { | |
| sess := sessions.NewFakeSession() | |
| sess.AnonymousStub = func() bool { return false } | |
| sess.HasRoleStub = func(...roles.Role) bool { return false } | |
| return sess | |
| } | |
| newCampusRepo = func() repos.CampusRepo { | |
| return repos.NewFakeCampusRepo() | |
| } | |
| w := httptest.NewRecorder() | |
| r := test_helpers.NewRequest("GET", "/admin/campuses") | |
| Index(w, r) | |
| assert.Equal(t, w.Code, status.Forbidden) | |
| } | |
| func Test_Index_OK(t *testing.T) { | |
| defer setDefaults() | |
| newSession = func(*http.Request) sessions.Session { | |
| sess := sessions.NewFakeSession() | |
| sess.AnonymousStub = func() bool { return false } | |
| sess.HasRoleStub = func(...roles.Role) bool { return true } | |
| sess.FirstPersonStub = func() (m models.FirstPerson) { return } | |
| return sess | |
| } | |
| newCampusRepo = func() repos.CampusRepo { | |
| repo := repos.NewFakeCampusRepo() | |
| repo.AllStub = func() (m models.Campuses) { | |
| return | |
| } | |
| return repo | |
| } | |
| w := httptest.NewRecorder() | |
| r := test_helpers.NewRequest("GET", "/admin/campuses") | |
| Index(w, r) | |
| assert.Equal(t, w.Code, status.OK) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package campuses | |
| import ( | |
| "fabric-server/repos" | |
| "fabric-server/web/sessions" | |
| "net/http" | |
| ) | |
| var newCampusRepo func() repos.CampusRepo | |
| var newSession func(*http.Request) sessions.Session | |
| func setDefaults() { | |
| newCampusRepo = repos.NewCampusRepo | |
| newSession = sessions.New | |
| } | |
| func init() { | |
| setDefaults() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment