Add test for github issue #399

Signed-off-by: Anant Prakash <anantprakashjsr@gmail.com>
This commit is contained in:
Anant Prakash 2018-03-14 19:47:00 +05:30
parent 369fb26a14
commit 5bbd593a48
No known key found for this signature in database
GPG key ID: C5D399F626523045
2 changed files with 16 additions and 4 deletions

View file

@ -50,7 +50,7 @@ const (
sessionIDLength = 24 sessionIDLength = 24
) )
// sessionsDict represents every sessions' completed flow stages. // sessionsDict keeps track of completed auth stages for each session.
type sessionsDict struct { type sessionsDict struct {
sessions map[string][]authtypes.LoginType sessions map[string][]authtypes.LoginType
} }
@ -60,16 +60,15 @@ func (d sessionsDict) GetCompletedStages(sessionID string) []authtypes.LoginType
if completedStages, ok := d.sessions[sessionID]; ok { if completedStages, ok := d.sessions[sessionID]; ok {
return completedStages return completedStages
} }
// Ensure that a empty slice is return and not nil. See gh #399. // Ensure that a empty slice is returned and not nil. See #399.
return make([]authtypes.LoginType, 0) return make([]authtypes.LoginType, 0)
} }
// AddCompletedStage adds a completed stage to the session. // AAddCompletedStage records that a session has completed an auth stage.
func (d *sessionsDict) AddCompletedStage(sessionID string, stage authtypes.LoginType) { func (d *sessionsDict) AddCompletedStage(sessionID string, stage authtypes.LoginType) {
d.sessions[sessionID] = append(d.GetCompletedStages(sessionID), stage) d.sessions[sessionID] = append(d.GetCompletedStages(sessionID), stage)
} }
// newSessionsDict returns a sessionsDict whose contained map is initialized and empty.
func newSessionsDict() *sessionsDict { func newSessionsDict() *sessionsDict {
return &sessionsDict{ return &sessionsDict{
sessions: make(map[string][]authtypes.LoginType), sessions: make(map[string][]authtypes.LoginType),

View file

@ -132,3 +132,16 @@ func TestFlowCheckingExtraneousIncorrectInput(t *testing.T) {
t.Error("Incorrect registration flow verification: ", testFlow, ", from allowed flows: ", allowedFlows, ". Should be false.") t.Error("Incorrect registration flow verification: ", testFlow, ", from allowed flows: ", allowedFlows, ". Should be false.")
} }
} }
// Completed flows stages should always be a valid slice header.
// TestEmptyCompletedFlows checks that sessionsDict returns a slice & not nil.
func TestEmptyCompletedFlows(t *testing.T) {
fakeEmptySessions := newSessionsDict()
fakeSessionID := "aRandomSessionIDWhichDoesNotExist"
ret := fakeEmptySessions.GetCompletedStages(fakeSessionID)
// check for []
if !(len(ret) == 0 && ret != nil) {
t.Error("Empty Completed Flow Stages should be a empty slice: returned ", ret, ". Should be []")
}
}