aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulien Dessaux2021-04-14 15:51:23 +0200
committerJulien Dessaux2021-04-14 15:51:23 +0200
commite4211d243221051f2367004225a05b13322bf8a9 (patch)
treebbc467de05a4ce065381464a5dcc22b0b8409c65
parentAdded a session table and a CreateSession function (diff)
downloadtrains-e4211d243221051f2367004225a05b13322bf8a9.tar.gz
trains-e4211d243221051f2367004225a05b13322bf8a9.tar.bz2
trains-e4211d243221051f2367004225a05b13322bf8a9.zip
Implemented the ResumeSession function
Diffstat (limited to '')
-rw-r--r--pkg/database/sessions.go25
-rw-r--r--pkg/database/sessions_test.go61
2 files changed, 86 insertions, 0 deletions
diff --git a/pkg/database/sessions.go b/pkg/database/sessions.go
index 72930f0..2db05c8 100644
--- a/pkg/database/sessions.go
+++ b/pkg/database/sessions.go
@@ -31,3 +31,28 @@ func (env *DBEnv) CreateSession(user *model.User) (*string, error) {
}
return &token, nil
}
+
+func (env *DBEnv) ResumeSession(token string) (*model.User, error) {
+ user := model.User{}
+ query := `
+ SELECT
+ id, username, email
+ FROM
+ users
+ INNER JOIN
+ sessions ON users.id = sessions.user_id
+ WHERE
+ sessions.token = $1;`
+ err := env.db.QueryRow(
+ query,
+ token,
+ ).Scan(
+ &user.Id,
+ &user.Username,
+ &user.Email,
+ )
+ if err != nil {
+ return nil, newQueryError("Could not run database query, most likely the token is invalid", err)
+ }
+ return &user, nil
+}
diff --git a/pkg/database/sessions_test.go b/pkg/database/sessions_test.go
index 0b6a370..c7820ee 100644
--- a/pkg/database/sessions_test.go
+++ b/pkg/database/sessions_test.go
@@ -87,3 +87,64 @@ func TestCreateSessionWithSQLMock(t *testing.T) {
})
}
}
+
+func TestResumeSession(t *testing.T) {
+ // test db setup : of the three users only two have session tokens
+ db, err := InitDB("sqlite3", "file::memory:?_foreign_keys=on")
+ require.NoError(t, err)
+ err = db.Migrate()
+ require.NoError(t, err)
+ userReg1 := model.UserRegistration{
+ Username: "user1",
+ Password: "user1_pass",
+ Email: "user1",
+ }
+ user1, err := db.CreateUser(&userReg1)
+ require.NoError(t, err)
+ token1, err := db.CreateSession(user1)
+ require.NoError(t, err)
+ token1bis, err := db.CreateSession(user1)
+ require.NoError(t, err)
+ userReg2 := model.UserRegistration{
+ Username: "user2",
+ Password: "user2_pass",
+ Email: "user2",
+ }
+ user2, err := db.CreateUser(&userReg2)
+ require.NoError(t, err)
+ token2, err := db.CreateSession(user2)
+ require.NoError(t, err)
+ userReg3 := model.UserRegistration{
+ Username: "user3",
+ Password: "user3_pass",
+ Email: "user3",
+ }
+ _, err = db.CreateUser(&userReg3)
+ require.NoError(t, err)
+ // Test cases
+ testCases := []struct {
+ name string
+ input string
+ expected *model.User
+ expectedError interface{}
+ }{
+ {"Normal user resume", *token1, user1, nil},
+ {"Normal user resume 1bis", *token1bis, user1, nil},
+ {"Normal user resume 2", *token2, user2, nil},
+ {"a non existant user token triggers an error", "XXX", nil, &QueryError{}},
+ }
+ for _, tc := range testCases {
+ t.Run(tc.name, func(t *testing.T) {
+ valid, err := db.ResumeSession(tc.input)
+ if tc.expectedError != nil {
+ require.Error(t, err)
+ assert.Equalf(t, reflect.TypeOf(err), reflect.TypeOf(tc.expectedError), "Invalid error type. Got %s but expected %s", reflect.TypeOf(err), reflect.TypeOf(tc.expectedError))
+ require.Nil(t, valid)
+ } else {
+ require.NoError(t, err)
+ assert.NotNil(t, valid)
+ assert.Equal(t, valid.Id, tc.expected.Id)
+ }
+ })
+ }
+}