|
| 1 | +package workers |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/go-co-op/gocron/v2" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | + |
| 13 | + "github.com/photoprism/photoprism/internal/auth/jwt" |
| 14 | + "github.com/photoprism/photoprism/internal/config" |
| 15 | +) |
| 16 | + |
| 17 | +// newJWTTestManager returns a key manager backed by a temp config directory. |
| 18 | +func newJWTTestManager(t *testing.T) *jwt.Manager { |
| 19 | + t.Helper() |
| 20 | + |
| 21 | + c := config.NewMinimalTestConfig(t.TempDir()) |
| 22 | + |
| 23 | + m, err := jwt.NewManager(c) |
| 24 | + require.NoError(t, err) |
| 25 | + |
| 26 | + t.Cleanup(func() { |
| 27 | + _ = os.RemoveAll(filepath.Join(c.PortalConfigPath(), "keys")) |
| 28 | + }) |
| 29 | + |
| 30 | + return m |
| 31 | +} |
| 32 | + |
| 33 | +func TestRunJWTKeyRotation(t *testing.T) { |
| 34 | + // The portal gate is what RunJWTKeyRotation adds over rotateJWTKeys, so it is what |
| 35 | + // these cases cover; the rotation itself is exercised through rotateJWTKeys below. |
| 36 | + t.Run("NilConfig", func(t *testing.T) { |
| 37 | + assert.NotPanics(t, func() { RunJWTKeyRotation(nil) }) |
| 38 | + }) |
| 39 | + t.Run("NotAPortal", func(t *testing.T) { |
| 40 | + c := config.NewMinimalTestConfig(t.TempDir()) |
| 41 | + require.False(t, c.Portal()) |
| 42 | + |
| 43 | + // Instances do not issue JWTs, so the manager must not even be resolved. |
| 44 | + resolved := false |
| 45 | + original := jwtManager |
| 46 | + jwtManager = func() *jwt.Manager { resolved = true; return nil } |
| 47 | + t.Cleanup(func() { jwtManager = original }) |
| 48 | + |
| 49 | + RunJWTKeyRotation(c) |
| 50 | + assert.False(t, resolved) |
| 51 | + }) |
| 52 | +} |
| 53 | + |
| 54 | +func TestRotateJWTKeys(t *testing.T) { |
| 55 | + t.Run("NoManager", func(t *testing.T) { |
| 56 | + assert.NotPanics(t, func() { rotateJWTKeys(nil, 90) }) |
| 57 | + }) |
| 58 | + t.Run("NotDue", func(t *testing.T) { |
| 59 | + m := newJWTTestManager(t) |
| 60 | + before, err := m.EnsureActiveKey() |
| 61 | + require.NoError(t, err) |
| 62 | + |
| 63 | + rotateJWTKeys(m, 90) |
| 64 | + |
| 65 | + after, err := m.ActiveKey() |
| 66 | + require.NoError(t, err) |
| 67 | + assert.Equal(t, before.Kid, after.Kid) |
| 68 | + assert.Len(t, m.JWKS().Keys, 1) |
| 69 | + }) |
| 70 | + t.Run("Disabled", func(t *testing.T) { |
| 71 | + m := newJWTTestManager(t) |
| 72 | + before, err := m.EnsureActiveKey() |
| 73 | + require.NoError(t, err) |
| 74 | + |
| 75 | + m.SetNow(func() time.Time { return time.Now().UTC().Add(10 * 365 * 24 * time.Hour) }) |
| 76 | + rotateJWTKeys(m, 0) |
| 77 | + |
| 78 | + after, err := m.ActiveKey() |
| 79 | + require.NoError(t, err) |
| 80 | + assert.Equal(t, before.Kid, after.Kid, "a disabled lifetime must not rotate, however old the key is") |
| 81 | + }) |
| 82 | + t.Run("Due", func(t *testing.T) { |
| 83 | + m := newJWTTestManager(t) |
| 84 | + before, err := m.EnsureActiveKey() |
| 85 | + require.NoError(t, err) |
| 86 | + |
| 87 | + // Move past the configured lifetime so the run is due. |
| 88 | + m.SetNow(func() time.Time { return time.Now().UTC().Add(91 * 24 * time.Hour) }) |
| 89 | + rotateJWTKeys(m, 90) |
| 90 | + |
| 91 | + after, err := m.ActiveKey() |
| 92 | + require.NoError(t, err) |
| 93 | + assert.NotEqual(t, before.Kid, after.Kid) |
| 94 | + assert.EqualValues(t, 0, after.NotAfter) |
| 95 | + // The replaced key keeps verifying during the overlap. |
| 96 | + assert.Len(t, m.JWKS().Keys, 2) |
| 97 | + }) |
| 98 | +} |
| 99 | + |
| 100 | +func TestJWTKeySchedule(t *testing.T) { |
| 101 | + // NewJob feeds this to gocron, which rejects a malformed expression at registration. |
| 102 | + scheduler, err := gocron.NewScheduler() |
| 103 | + require.NoError(t, err) |
| 104 | + t.Cleanup(func() { _ = scheduler.Shutdown() }) |
| 105 | + |
| 106 | + _, err = scheduler.NewJob(gocron.CronJob(JWTKeySchedule, false), gocron.NewTask(func() {})) |
| 107 | + assert.NoError(t, err) |
| 108 | +} |
0 commit comments