package store_test import ( "context" "errors" "testing" "time" "github.com/netcell-it/deklarix/internal/store" ) func TestPlatformConnectionUpsertGetDelete(t *testing.T) { s := openTestStore(t) ctx := context.Background() accID := testAccountID(t, s) expires := time.Now().Add(60 * 24 * time.Hour).Truncate(time.Millisecond) c, err := s.UpsertPlatformConnection(ctx, accID, "instagram", "ig-user-1", "access-1", "", &expires) if err != nil { t.Fatalf("UpsertPlatformConnection: %v", err) } if c.Platform != "instagram" || c.PlatformUserID != "ig-user-1" { t.Fatalf("UpsertPlatformConnection = %+v, unerwartete Werte", c) } got, err := s.GetPlatformConnection(ctx, accID, "instagram") if err != nil { t.Fatalf("GetPlatformConnection: %v", err) } if got.AccessToken != "access-1" { t.Fatalf("AccessToken = %q, want access-1", got.AccessToken) } list, err := s.ListPlatformConnectionsForAccount(ctx, accID) if err != nil { t.Fatalf("ListPlatformConnectionsForAccount: %v", err) } if len(list) != 1 { t.Fatalf("expected exactly 1 connection, got %d", len(list)) } if err := s.DeletePlatformConnection(ctx, accID, "instagram"); err != nil { t.Fatalf("DeletePlatformConnection: %v", err) } if _, err := s.GetPlatformConnection(ctx, accID, "instagram"); !errors.Is(err, store.ErrNotFound) { t.Fatalf("err nach Delete = %v, want store.ErrNotFound", err) } } func TestPlatformConnectionUpsertReplacesExisting(t *testing.T) { s := openTestStore(t) ctx := context.Background() accID := testAccountID(t, s) first, err := s.UpsertPlatformConnection(ctx, accID, "tiktok", "tt-user-1", "access-alt", "refresh-alt", nil) if err != nil { t.Fatalf("UpsertPlatformConnection (1): %v", err) } second, err := s.UpsertPlatformConnection(ctx, accID, "tiktok", "tt-user-1", "access-neu", "refresh-neu", nil) if err != nil { t.Fatalf("UpsertPlatformConnection (2): %v", err) } if second.ID != first.ID { t.Fatalf("expected the same row to be updated (same account+platform), got a new ID") } if second.AccessToken != "access-neu" || second.RefreshToken != "refresh-neu" { t.Fatalf("UpsertPlatformConnection (2) = %+v, tokens wurden nicht ersetzt", second) } list, err := s.ListPlatformConnectionsForAccount(ctx, accID) if err != nil { t.Fatalf("ListPlatformConnectionsForAccount: %v", err) } if len(list) != 1 { t.Fatalf("expected exactly 1 connection after upsert-replace, got %d", len(list)) } } func TestGetPlatformConnectionNotFound(t *testing.T) { s := openTestStore(t) ctx := context.Background() accID := testAccountID(t, s) _, err := s.GetPlatformConnection(ctx, accID, "instagram") if !errors.Is(err, store.ErrNotFound) { t.Fatalf("err = %v, want store.ErrNotFound", err) } } func TestDeletePlatformConnectionNotFound(t *testing.T) { s := openTestStore(t) ctx := context.Background() accID := testAccountID(t, s) err := s.DeletePlatformConnection(ctx, accID, "tiktok") if !errors.Is(err, store.ErrNotFound) { t.Fatalf("err = %v, want store.ErrNotFound", err) } } func TestPlatformConnectionIsolatedPerAccount(t *testing.T) { s := openTestStore(t) ctx := context.Background() accA := testAccountID(t, s) accB := testAccountID(t, s) if _, err := s.UpsertPlatformConnection(ctx, accA, "instagram", "ig-a", "token-a", "", nil); err != nil { t.Fatalf("UpsertPlatformConnection (A): %v", err) } if _, err := s.GetPlatformConnection(ctx, accB, "instagram"); !errors.Is(err, store.ErrNotFound) { t.Fatalf("Mandant B sollte keine Verbindung von Mandant A sehen, err = %v", err) } }