Commit adbb3508 authored by Akshay Bharambe's avatar Akshay Bharambe
Browse files

Add: Tests for session control

parent 0b0a00d4
Branches
Tags
2 merge requests!210Staging mepdeployment05072020,!200Add: Session control
Showing with 147 additions and 0 deletions
package sessionmdl
import (
"testing"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/cachemdl"
)
func init() {
InitSessionManagerCache(cachemdl.TypeFastCache)
}
func TestSet(t *testing.T) {
Set("tom@company.org", Session{SessionFor: "xyz", SessionId: "789"})
type args struct {
userId string
s []Session
}
tests := []struct {
name string
args args
}{
// TODO: Add test cases.
{
name: "User present",
args: args{
s: []Session{Session{SessionFor: "abc", SessionId: "123"}},
userId: "tom@company.org",
},
},
{
name: "User not present",
args: args{
s: []Session{Session{SessionFor: "abc", SessionId: "123"}},
userId: "ronny@company.org",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Set(tt.args.userId, tt.args.s...)
})
}
all := store.GetItemsCount()
if all != len(tests) {
t.Errorf("expected %d users got %d", len(tests), all)
}
}
func TestValidateSessionFromToken(t *testing.T) {
Set("tom@company.org", Session{SessionFor: "xyz", SessionId: "789"})
type args struct {
claims map[string]interface{}
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Session present",
args: args{claims: map[string]interface{}{"userId": "tom@company.org", "sessionId": "789"}},
wantErr: false,
},
{
name: "Session not present",
args: args{claims: map[string]interface{}{"userId": "invalid@company.org", "sessionId": "123"}},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := ValidateSessionFromToken(tt.args.claims); (err != nil) != tt.wantErr {
t.Errorf("ValidateSessionFromToken() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestCheckForSessionAvailability(t *testing.T) {
Set("tom@company.org", Session{SessionFor: "xyz", SessionId: "789"})
type args struct {
userId string
sessionFor string
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Session present",
args: args{userId: "tom@company.org", sessionFor: "xyz"},
wantErr: false,
},
{
name: "Session not present",
args: args{userId: "tom@company.org", sessionFor: "someRandomID"},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := CheckForSessionAvailability(tt.args.userId, tt.args.sessionFor); (err != nil) != tt.wantErr {
t.Errorf("CheckForSessionAvailability() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestDeleteSession(t *testing.T) {
Set("TestDeleteSession", Session{SessionFor: "xyz", SessionId: "789"})
Set("TestDeleteSession", Session{SessionFor: "abc", SessionId: "123"})
type args struct {
userId string
sessionFor string
}
tests := []struct {
name string
args args
}{
{
name: "Delete existing session",
args: args{userId: "TestDeleteSession", sessionFor: "xyz"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
DeleteSession(tt.args.userId, tt.args.sessionFor)
i, _ := store.Get(tt.args.userId)
// if !ok {
// t.Error("expected", tt.args.userId, "not to be deleted")
// }
sessions := i.([]Session)
for _, s := range sessions {
if s.SessionFor == tt.args.sessionFor {
t.Error("expected", tt.args.sessionFor, "to be deleted")
}
}
})
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment