Skip to content
GitLab
Explore
Projects
Groups
Topics
Snippets
Projects
Groups
Topics
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
MKCLOS
Core Development Platform
corepkgv2
Commits
adbb3508
Commit
adbb3508
authored
4 years ago
by
Akshay Bharambe
Browse files
Options
Downloads
Patches
Plain Diff
Add: Tests for session control
parent
0b0a00d4
Branches
Branches containing commit
Tags
Tags containing commit
2 merge requests
!210
Staging mepdeployment05072020
,
!200
Add: Session control
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
authmdl/sessionmdl/session_test.go
+147
-0
authmdl/sessionmdl/session_test.go
with
147 additions
and
0 deletions
authmdl/sessionmdl/session_test.go
0 → 100644
+
147
−
0
View file @
adbb3508
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"
)
}
}
})
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment
Menu
Explore
Projects
Groups
Topics
Snippets