From 5bbd593a486433d6c9a4ab383457021cfed4f79d Mon Sep 17 00:00:00 2001 From: Anant Prakash Date: Wed, 14 Mar 2018 19:47:00 +0530 Subject: [PATCH] Add test for github issue #399 Signed-off-by: Anant Prakash --- .../dendrite/clientapi/routing/register.go | 7 +++---- .../dendrite/clientapi/routing/register_test.go | 13 +++++++++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/github.com/matrix-org/dendrite/clientapi/routing/register.go b/src/github.com/matrix-org/dendrite/clientapi/routing/register.go index 8d3ca2247..11dca0ec3 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/routing/register.go +++ b/src/github.com/matrix-org/dendrite/clientapi/routing/register.go @@ -50,7 +50,7 @@ const ( sessionIDLength = 24 ) -// sessionsDict represents every sessions' completed flow stages. +// sessionsDict keeps track of completed auth stages for each session. type sessionsDict struct { sessions map[string][]authtypes.LoginType } @@ -60,16 +60,15 @@ func (d sessionsDict) GetCompletedStages(sessionID string) []authtypes.LoginType if completedStages, ok := d.sessions[sessionID]; ok { 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) } -// 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) { d.sessions[sessionID] = append(d.GetCompletedStages(sessionID), stage) } -// newSessionsDict returns a sessionsDict whose contained map is initialized and empty. func newSessionsDict() *sessionsDict { return &sessionsDict{ sessions: make(map[string][]authtypes.LoginType), diff --git a/src/github.com/matrix-org/dendrite/clientapi/routing/register_test.go b/src/github.com/matrix-org/dendrite/clientapi/routing/register_test.go index 0fae27e9c..7e3b893ba 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/routing/register_test.go +++ b/src/github.com/matrix-org/dendrite/clientapi/routing/register_test.go @@ -132,3 +132,16 @@ func TestFlowCheckingExtraneousIncorrectInput(t *testing.T) { 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 []") + } +}