(t *testing.T)
| 16 | ) |
| 17 | |
| 18 | func TestRegenerateVapidKeypair(t *testing.T) { |
| 19 | t.Parallel() |
| 20 | |
| 21 | t.Run("NoExistingVAPIDKeys", func(t *testing.T) { |
| 22 | t.Parallel() |
| 23 | |
| 24 | ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort) |
| 25 | t.Cleanup(cancel) |
| 26 | |
| 27 | connectionURL, err := dbtestutil.Open(t) |
| 28 | require.NoError(t, err) |
| 29 | |
| 30 | sqlDB, err := sql.Open("postgres", connectionURL) |
| 31 | require.NoError(t, err) |
| 32 | defer sqlDB.Close() |
| 33 | |
| 34 | db := database.New(sqlDB) |
| 35 | // Ensure there is no existing VAPID keypair. |
| 36 | rows, err := db.GetWebpushVAPIDKeys(ctx) |
| 37 | require.NoError(t, err) |
| 38 | require.Empty(t, rows) |
| 39 | |
| 40 | inv, _ := clitest.New(t, "server", "regenerate-vapid-keypair", "--postgres-url", connectionURL, "--yes") |
| 41 | |
| 42 | pty := ptytest.New(t) |
| 43 | inv.Stdout = pty.Output() |
| 44 | inv.Stderr = pty.Output() |
| 45 | clitest.Start(t, inv) |
| 46 | |
| 47 | pty.ExpectMatchContext(ctx, "Regenerating VAPID keypair...") |
| 48 | pty.ExpectMatchContext(ctx, "This will delete all existing webpush subscriptions.") |
| 49 | pty.ExpectMatchContext(ctx, "Are you sure you want to continue? (y/N)") |
| 50 | pty.WriteLine("y") |
| 51 | pty.ExpectMatchContext(ctx, "VAPID keypair regenerated successfully.") |
| 52 | |
| 53 | // Ensure the VAPID keypair was created. |
| 54 | keys, err := db.GetWebpushVAPIDKeys(ctx) |
| 55 | require.NoError(t, err) |
| 56 | require.NotEmpty(t, keys.VapidPublicKey) |
| 57 | require.NotEmpty(t, keys.VapidPrivateKey) |
| 58 | }) |
| 59 | |
| 60 | t.Run("ExistingVAPIDKeys", func(t *testing.T) { |
| 61 | t.Parallel() |
| 62 | |
| 63 | ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort) |
| 64 | t.Cleanup(cancel) |
| 65 | |
| 66 | connectionURL, err := dbtestutil.Open(t) |
| 67 | require.NoError(t, err) |
| 68 | |
| 69 | sqlDB, err := sql.Open("postgres", connectionURL) |
| 70 | require.NoError(t, err) |
| 71 | defer sqlDB.Close() |
| 72 | |
| 73 | db := database.New(sqlDB) |
| 74 | for i := 0; i < 10; i++ { |
| 75 | // Insert a few fake users. |
nothing calls this directly
no test coverage detected