test(auth): add session expiry test with short ttl
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
//go:build failure
|
||||
|
||||
package studenttests_test
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"os/exec"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
const shortTTLContainer = "api-short-ttl"
|
||||
const shortTTLURL = "http://localhost:8081"
|
||||
|
||||
func TestSessionExpiry(t *testing.T) {
|
||||
_ = exec.Command("docker", "rm", "-f", shortTTLContainer).Run()
|
||||
|
||||
compose(t, "run", "-d", "--no-deps", "--name", shortTTLContainer,
|
||||
"-e", "SESSION_TTL_SECONDS=3",
|
||||
"-p", "127.0.0.1:8081:8080", "api")
|
||||
t.Cleanup(func() {
|
||||
_ = exec.Command("docker", "rm", "-f", shortTTLContainer).Run()
|
||||
})
|
||||
|
||||
old := *baseURL
|
||||
*baseURL = shortTTLURL
|
||||
t.Cleanup(func() { *baseURL = old })
|
||||
|
||||
waitReady(t)
|
||||
|
||||
user := createUser(t)
|
||||
|
||||
resp := doRequest(t, http.MethodGet, "/api/v1/users/me", user.Token, nil)
|
||||
requireStatus(t, resp, http.StatusOK)
|
||||
resp.Body.Close()
|
||||
|
||||
time.Sleep(4 * time.Second)
|
||||
|
||||
resp = doRequest(t, http.MethodGet, "/api/v1/users/me", user.Token, nil)
|
||||
requireStatus(t, resp, http.StatusUnauthorized)
|
||||
resp.Body.Close()
|
||||
}
|
||||
Reference in New Issue
Block a user