From 5d5ce8ae5e365dea513b4da66aff06957a64bef4 Mon Sep 17 00:00:00 2001 From: Lukasz Raczylo Date: Wed, 19 Feb 2025 12:08:37 +0000 Subject: [PATCH] Additional tests for the blacklists --- blacklist_test.go | 74 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 blacklist_test.go diff --git a/blacklist_test.go b/blacklist_test.go new file mode 100644 index 0000000..571348c --- /dev/null +++ b/blacklist_test.go @@ -0,0 +1,74 @@ +package traefikoidc + +import ( + "testing" + "time" +) + +func TestTokenBlacklist_Add(t *testing.T) { + blacklist := NewTokenBlacklist() + token := "testToken" + expiry := time.Now().Add(time.Hour) + + blacklist.Add(token, expiry) + + if !blacklist.IsBlacklisted(token) { + t.Errorf("Expected token to be blacklisted, but it was not") + } +} + +func TestTokenBlacklist_IsBlacklisted(t *testing.T) { + blacklist := NewTokenBlacklist() + token := "testToken" + expiry := time.Now().Add(time.Hour) + + blacklist.Add(token, expiry) + + if !blacklist.IsBlacklisted(token) { + t.Errorf("Expected token to be blacklisted, but it was not") + } + + if blacklist.IsBlacklisted("nonExistentToken") { + t.Errorf("Expected non-existent token to not be blacklisted, but it was") + } +} + +func TestTokenBlacklist_Cleanup(t *testing.T) { + blacklist := NewTokenBlacklist() + token := "testToken" + expiry := time.Now().Add(-time.Hour) // Expired token + + blacklist.Add(token, expiry) + blacklist.Cleanup() + + if blacklist.IsBlacklisted(token) { + t.Errorf("Expected expired token to be removed after cleanup, but it was not") + } +} + +func TestTokenBlacklist_Remove(t *testing.T) { + blacklist := NewTokenBlacklist() + token := "testToken" + expiry := time.Now().Add(time.Hour) + + blacklist.Add(token, expiry) + blacklist.Remove(token) + + if blacklist.IsBlacklisted(token) { + t.Errorf("Expected token to be removed, but it was not") + } +} + +func TestTokenBlacklist_Count(t *testing.T) { + blacklist := NewTokenBlacklist() + token1 := "token1" + token2 := "token2" + expiry := time.Now().Add(time.Hour) + + blacklist.Add(token1, expiry) + blacklist.Add(token2, expiry) + + if blacklist.Count() != 2 { + t.Errorf("Expected blacklist count to be 2, but got %d", blacklist.Count()) + } +}