From 464b1d6e429f9120b88dc6e2d82c5dbf689069ed Mon Sep 17 00:00:00 2001 From: Justin Date: Sat, 18 Oct 2025 15:38:51 -0500 Subject: [PATCH 1/3] Creates methods_test.go from utils_test.go with additional tests --- utils/methods_test.go | 114 ++++++++++++++++++++++++++++++++++++++++++ utils/utils_test.go | 52 ------------------- 2 files changed, 114 insertions(+), 52 deletions(-) create mode 100644 utils/methods_test.go delete mode 100644 utils/utils_test.go diff --git a/utils/methods_test.go b/utils/methods_test.go new file mode 100644 index 0000000..849f7f6 --- /dev/null +++ b/utils/methods_test.go @@ -0,0 +1,114 @@ +package utils + +import ( + "log" + "os" + "strings" + "testing" + + "github.com/joho/godotenv" +) + +// TestMain loads environment variables for utils package tests. +func TestMain(m *testing.M) { + // Load .env vars for testing + err := godotenv.Load("../.env") + if err != nil { + log.Fatalf("Error loading .env file: %v", err) + } + + // The following environment variables must be set for tests to run + envVars := []string{ + "LOGIN_NETID", "LOGIN_PASSWORD", "LOGIN_ASTRA_USERNAME", "LOGIN_ASTRA_PASSWORD", + } + + for _, envVar := range envVars { + // Get each env, and if it doesn't exist, error + v, err := GetEnv(envVar) + if v == "" || err != nil { + log.Fatalf("Error loading environment variable %v: %v\n"+ + "Valid credentials are required for testing utils/methods", envVar, err) + } + } + + // Run tests + os.Exit(m.Run()) +} + +func TestInitChromeDP(t *testing.T) { + // Test to run in both headless and non-headless + test := func(t *testing.T) { + ctx, cancel := InitChromeDp() + defer cancel() + if ctx.Err() != nil { + t.Errorf("Failed to initialize chromedp: %v", ctx.Err()) + } + } + + Headless = false + t.Run("not-headless", test) + + Headless = true + t.Run("headless", test) + +} + +// TestRefreshToken confirms coursebook tokens refresh under both headless settings. +func TestRefreshToken(t *testing.T) { + // Get a chromedp context + ctx, cancel := InitChromeDp() + defer cancel() + // Try refreshing token + headers := RefreshToken(ctx) + + // Make sure we successfully got a PTGSESSID cookie + // Check to make sure we have a cookie + found := false + for _, cookie := range headers["Cookie"] { + if strings.HasPrefix(cookie, "PTGSESSID") { + found = true + } + } + + // Fail if no PTGSESSID cookie found + if !found { + t.Errorf("Failed to get PTGSESSID cookie from RefreshToken!") + } +} + +// TestRefreshAstraToken test RefreshAstraToken returns the proper cookie +func TestRefreshAstraToken(t *testing.T) { + ctx, cancel := InitChromeDp() + defer cancel() + // Try refreshing token + headers := RefreshAstraToken(ctx) + + // Make sure we successfully got a PTGSESSID cookie + // Check to make sure we have a cookie + found := false + // Split cookie string to get each cookie + cookies := strings.Split(headers["Cookie"][0], "; ") + for _, cookie := range cookies { + if strings.HasPrefix(cookie, "UTXDallas.ASPXFORMSAUTH") { + found = true + } + } + + // Fail if cookie not found + if !found { + t.Errorf("Failed to get UTXDallas.ASPXFORMSAUTH cookie from RefreshAstraToken!") + } +} + +// TestGetCoursePrefixes tests GetCoursePrefixes at least 20 course prefixes +func TestGetCoursePrefixes(t *testing.T) { + ctx, cancel := InitChromeDp() + defer cancel() + prefixes := GetCoursePrefixes(ctx) + t.Log(len(prefixes)) + + // There should be at least 20 course prefixes + if len(prefixes) < 20 { + t.Errorf("Failed to fetch at least 20 course prefixes. Found %d", len(prefixes)) + } +} diff --git a/utils/utils_test.go b/utils/utils_test.go deleted file mode 100644 index aeb5ba2..0000000 --- a/utils/utils_test.go +++ /dev/null @@ -1,52 +0,0 @@ -package utils - -import ( - "os" - "strings" - "testing" - - "github.com/joho/godotenv" -) - -// TestMain loads environment variables for utils package tests. -func TestMain(m *testing.M) { - // Load .env vars for testing - godotenv.Load("../.env") - // Run tests - os.Exit(m.Run()) -} - -// TestInitChromeDp ensures chromedp contexts initialize in both headed and headless modes. -func TestInitChromeDp(t *testing.T) { - // Test with head - Headless = false - ctx, cancel := InitChromeDp() - if ctx.Err() != nil { - t.Errorf("Failed to initialize chromedp with head") - } - cancel() - // Test headless - Headless = true - ctx, cancel = InitChromeDp() - if ctx.Err() != nil { - t.Errorf("Failed to initialize chromedp headlessly") - } - cancel() -} - -// TestRefreshToken confirms coursebook tokens refresh under both headless settings. -func TestRefreshToken(t *testing.T) { - // Get a chromedp context - ctx, cancel := InitChromeDp() - defer cancel() - // Try refreshing token - headers := RefreshToken(ctx) - // Make sure we successfully got a PTGSESSID cookie - for _, cookie := range headers["Cookie"] { - if strings.HasPrefix(cookie, "PTGSESSID") { - return - } - } - // Fail if no PTGSESSID cookie found - t.Fatalf("Failed to get PTGSESSID cookie from RefreshToken!") -} From 46fd810fa8621e35d3faf3a4858023954b0af5f5 Mon Sep 17 00:00:00 2001 From: Justin Date: Sat, 18 Oct 2025 15:39:07 -0500 Subject: [PATCH 2/3] Adds extra WriteJSON errors --- utils/methods.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/utils/methods.go b/utils/methods.go index 90712e2..911e55d 100644 --- a/utils/methods.go +++ b/utils/methods.go @@ -221,10 +221,18 @@ func WriteJSON(filepath string, data interface{}) error { if err != nil { return err } - defer fptr.Close() + //defer fptr.Close() encoder := json.NewEncoder(fptr) encoder.SetIndent("", "\t") - encoder.Encode(data) + err = encoder.Encode(data) + if err != nil { + return fmt.Errorf("Failed to encode JSON for %v: %v", filepath, err) + } + + err = fptr.Close() + if err != nil { + return fmt.Errorf("Failed to close JSON file %v: %v", filepath, err) + } return nil } From 6714bea8cbcf2f20e85a8df01b2f379a653ebd45 Mon Sep 17 00:00:00 2001 From: Justin Date: Sat, 18 Oct 2025 15:50:10 -0500 Subject: [PATCH 3/3] Fixes errors starting with capital letter --- utils/methods.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/utils/methods.go b/utils/methods.go index 911e55d..e6fd756 100644 --- a/utils/methods.go +++ b/utils/methods.go @@ -226,12 +226,12 @@ func WriteJSON(filepath string, data interface{}) error { encoder.SetIndent("", "\t") err = encoder.Encode(data) if err != nil { - return fmt.Errorf("Failed to encode JSON for %v: %v", filepath, err) + return fmt.Errorf("failed to encode JSON for %v: %v", filepath, err) } err = fptr.Close() if err != nil { - return fmt.Errorf("Failed to close JSON file %v: %v", filepath, err) + return fmt.Errorf("failed to close JSON file %v: %v", filepath, err) } return nil }