|
| 1 | +package root |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/base64" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +func buildTestJWT(claims map[string]any) string { |
| 16 | + header := base64.RawURLEncoding.EncodeToString([]byte(`{"alg":"HS256","typ":"JWT"}`)) |
| 17 | + payload, _ := json.Marshal(claims) |
| 18 | + payloadB64 := base64.RawURLEncoding.EncodeToString(payload) |
| 19 | + sig := base64.RawURLEncoding.EncodeToString([]byte("fakesig")) |
| 20 | + return fmt.Sprintf("%s.%s.%s", header, payloadB64, sig) |
| 21 | +} |
| 22 | + |
| 23 | +func TestParseAuthInfo_ValidToken(t *testing.T) { |
| 24 | + t.Parallel() |
| 25 | + |
| 26 | + now := time.Now() |
| 27 | + exp := now.Add(time.Hour) |
| 28 | + token := buildTestJWT(map[string]any{ |
| 29 | + "sub": "user-123", |
| 30 | + "iss": "docker", |
| 31 | + "iat": now.Unix(), |
| 32 | + "exp": exp.Unix(), |
| 33 | + }) |
| 34 | + |
| 35 | + info, err := parseAuthInfo(token) |
| 36 | + require.NoError(t, err) |
| 37 | + assert.Equal(t, token, info.Token) |
| 38 | + assert.Equal(t, "user-123", info.Subject) |
| 39 | + assert.Equal(t, "docker", info.Issuer) |
| 40 | + assert.False(t, info.Expired) |
| 41 | + assert.WithinDuration(t, now, info.IssuedAt, time.Second) |
| 42 | + assert.WithinDuration(t, exp, info.ExpiresAt, time.Second) |
| 43 | +} |
| 44 | + |
| 45 | +func TestParseAuthInfo_ExpiredToken(t *testing.T) { |
| 46 | + t.Parallel() |
| 47 | + |
| 48 | + exp := time.Now().Add(-time.Hour) |
| 49 | + token := buildTestJWT(map[string]any{ |
| 50 | + "sub": "user-456", |
| 51 | + "exp": exp.Unix(), |
| 52 | + }) |
| 53 | + |
| 54 | + info, err := parseAuthInfo(token) |
| 55 | + require.NoError(t, err) |
| 56 | + assert.True(t, info.Expired) |
| 57 | + assert.Equal(t, "user-456", info.Subject) |
| 58 | +} |
| 59 | + |
| 60 | +func TestParseAuthInfo_InvalidToken(t *testing.T) { |
| 61 | + t.Parallel() |
| 62 | + |
| 63 | + _, err := parseAuthInfo("not-a-jwt") |
| 64 | + require.Error(t, err) |
| 65 | +} |
| 66 | + |
| 67 | +func TestPrintAuthInfoText(t *testing.T) { |
| 68 | + t.Parallel() |
| 69 | + |
| 70 | + info := &authInfo{ |
| 71 | + Token: "eyJhbGciOiJIUzI1NiJ9.xxxxxxxxxxxx.yyyyyyyy1234567890", |
| 72 | + Username: "testuser", |
| 73 | + Email: "test@example.com", |
| 74 | + Subject: "sub-123", |
| 75 | + Issuer: "docker", |
| 76 | + IssuedAt: time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC), |
| 77 | + ExpiresAt: time.Date(2099, 1, 1, 0, 0, 0, 0, time.UTC), |
| 78 | + Expired: false, |
| 79 | + } |
| 80 | + |
| 81 | + var buf bytes.Buffer |
| 82 | + printAuthInfoText(&buf, info) |
| 83 | + |
| 84 | + output := buf.String() |
| 85 | + assert.Contains(t, output, "testuser") |
| 86 | + assert.Contains(t, output, "test@example.com") |
| 87 | + assert.Contains(t, output, "sub-123") |
| 88 | + assert.Contains(t, output, "docker") |
| 89 | + assert.Contains(t, output, "✅ Valid") |
| 90 | +} |
| 91 | + |
| 92 | +func TestPrintAuthInfoText_Expired(t *testing.T) { |
| 93 | + t.Parallel() |
| 94 | + |
| 95 | + info := &authInfo{ |
| 96 | + Token: "eyJhbGciOiJIUzI1NiJ9.xxxxxxxxxxxx.yyyyyyyy1234567890", |
| 97 | + ExpiresAt: time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC), |
| 98 | + Expired: true, |
| 99 | + } |
| 100 | + |
| 101 | + var buf bytes.Buffer |
| 102 | + printAuthInfoText(&buf, info) |
| 103 | + |
| 104 | + assert.Contains(t, buf.String(), "❌ Expired") |
| 105 | +} |
0 commit comments