|
| 1 | +package slack |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/codeGROOVE-dev/slacker/pkg/state" |
| 12 | + "github.com/slack-go/slack" |
| 13 | +) |
| 14 | + |
| 15 | +// TestVerifyRequest_ValidBody tests request verification with valid body. |
| 16 | +func TestVerifyRequest_ValidBody(t *testing.T) { |
| 17 | + t.Parallel() |
| 18 | + |
| 19 | + client := &Client{ |
| 20 | + signingSecret: "test-secret", |
| 21 | + } |
| 22 | + |
| 23 | + body := []byte("test body content") |
| 24 | + req := httptest.NewRequest(http.MethodPost, "/test", bytes.NewReader(body)) |
| 25 | + req.Header.Set("X-Slack-Request-Timestamp", "1234567890") |
| 26 | + req.Header.Set("X-Slack-Signature", "v0=invalid") |
| 27 | + |
| 28 | + // This will fail verification but test the body reading logic |
| 29 | + _ = client.verifyRequest(req) |
| 30 | + |
| 31 | + // Verify body can be read again |
| 32 | + readBody, err := io.ReadAll(req.Body) |
| 33 | + if err != nil { |
| 34 | + t.Fatal("Failed to read body after verification:", err) |
| 35 | + } |
| 36 | + if !bytes.Equal(readBody, body) { |
| 37 | + t.Error("Body was not properly restored") |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +// TestVerifyRequest_EmptyBody tests request verification with empty body. |
| 42 | +func TestVerifyRequest_EmptyBody(t *testing.T) { |
| 43 | + t.Parallel() |
| 44 | + |
| 45 | + client := &Client{ |
| 46 | + signingSecret: "test-secret", |
| 47 | + } |
| 48 | + |
| 49 | + req := httptest.NewRequest(http.MethodPost, "/test", http.NoBody) |
| 50 | + req.Header.Set("X-Slack-Request-Timestamp", "1234567890") |
| 51 | + req.Header.Set("X-Slack-Signature", "v0=test") |
| 52 | + |
| 53 | + _ = client.verifyRequest(req) |
| 54 | + // Test completes if it doesn't panic |
| 55 | +} |
| 56 | + |
| 57 | +// TestUserInfo_Success tests getting user info successfully. |
| 58 | +func TestUserInfo_Success(t *testing.T) { |
| 59 | + t.Parallel() |
| 60 | + |
| 61 | + mockAPI := &mockAPI{ |
| 62 | + getUserInfoFunc: func(ctx context.Context, userID string) (*slack.User, error) { |
| 63 | + return &slack.User{ |
| 64 | + ID: userID, |
| 65 | + Name: "testuser", |
| 66 | + TZ: "America/Los_Angeles", |
| 67 | + }, nil |
| 68 | + }, |
| 69 | + } |
| 70 | + |
| 71 | + client := &Client{ |
| 72 | + api: mockAPI, |
| 73 | + cache: &apiCache{ |
| 74 | + entries: make(map[string]cacheEntry), |
| 75 | + }, |
| 76 | + } |
| 77 | + |
| 78 | + user, err := client.UserInfo(context.Background(), "U123") |
| 79 | + |
| 80 | + if err != nil { |
| 81 | + t.Fatalf("Expected no error, got: %v", err) |
| 82 | + } |
| 83 | + |
| 84 | + if user.ID != "U123" { |
| 85 | + t.Errorf("Expected user ID U123, got %s", user.ID) |
| 86 | + } |
| 87 | + |
| 88 | + if user.Name != "testuser" { |
| 89 | + t.Errorf("Expected user name testuser, got %s", user.Name) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +// TestUserTimezone_Valid tests getting user timezone. |
| 94 | +func TestUserTimezone_Valid(t *testing.T) { |
| 95 | + t.Parallel() |
| 96 | + |
| 97 | + mockAPI := &mockAPI{ |
| 98 | + getUserInfoFunc: func(ctx context.Context, userID string) (*slack.User, error) { |
| 99 | + return &slack.User{ |
| 100 | + ID: userID, |
| 101 | + TZ: "America/Los_Angeles", |
| 102 | + }, nil |
| 103 | + }, |
| 104 | + } |
| 105 | + |
| 106 | + client := &Client{ |
| 107 | + api: mockAPI, |
| 108 | + cache: &apiCache{ |
| 109 | + entries: make(map[string]cacheEntry), |
| 110 | + }, |
| 111 | + } |
| 112 | + |
| 113 | + tz, err := client.UserTimezone(context.Background(), "U123") |
| 114 | + |
| 115 | + if err != nil { |
| 116 | + t.Fatalf("Expected no error, got: %v", err) |
| 117 | + } |
| 118 | + |
| 119 | + if tz != "America/Los_Angeles" { |
| 120 | + t.Errorf("Expected America/Los_Angeles, got %s", tz) |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +// TestClient_SetManager tests setting the manager reference. |
| 125 | +func TestClient_SetManager(t *testing.T) { |
| 126 | + t.Parallel() |
| 127 | + |
| 128 | + client := &Client{} |
| 129 | + manager := NewManager("test-secret") |
| 130 | + |
| 131 | + client.SetManager(manager) |
| 132 | + |
| 133 | + if client.manager != manager { |
| 134 | + t.Error("Expected manager to be set") |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +// TestClient_SetTeamID tests setting team ID. |
| 139 | +func TestClient_SetTeamID(t *testing.T) { |
| 140 | + t.Parallel() |
| 141 | + |
| 142 | + client := &Client{} |
| 143 | + client.SetTeamID("T123") |
| 144 | + |
| 145 | + if client.teamID != "T123" { |
| 146 | + t.Errorf("Expected team ID T123, got %s", client.teamID) |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +// TestClient_SetStateStore tests setting state store. |
| 151 | +func TestClient_SetStateStore(t *testing.T) { |
| 152 | + t.Parallel() |
| 153 | + |
| 154 | + client := &Client{} |
| 155 | + store := &state.MemoryStore{} |
| 156 | + |
| 157 | + client.SetStateStore(store) |
| 158 | + |
| 159 | + client.stateStoreMu.RLock() |
| 160 | + gotStore := client.stateStore |
| 161 | + client.stateStoreMu.RUnlock() |
| 162 | + |
| 163 | + if gotStore != store { |
| 164 | + t.Error("Expected state store to be set") |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +// TestClient_SetHomeViewHandler tests setting home view handler. |
| 169 | +func TestClient_SetHomeViewHandler(t *testing.T) { |
| 170 | + t.Parallel() |
| 171 | + |
| 172 | + client := &Client{} |
| 173 | + called := false |
| 174 | + handler := func(ctx context.Context, teamID, userID string) error { |
| 175 | + called = true |
| 176 | + return nil |
| 177 | + } |
| 178 | + |
| 179 | + client.SetHomeViewHandler(handler) |
| 180 | + |
| 181 | + client.homeViewHandlerMu.RLock() |
| 182 | + gotHandler := client.homeViewHandler |
| 183 | + client.homeViewHandlerMu.RUnlock() |
| 184 | + |
| 185 | + if gotHandler == nil { |
| 186 | + t.Fatal("Expected handler to be set") |
| 187 | + } |
| 188 | + |
| 189 | + _ = gotHandler(context.Background(), "T123", "U123") |
| 190 | + |
| 191 | + if !called { |
| 192 | + t.Error("Expected handler to be called") |
| 193 | + } |
| 194 | +} |
| 195 | + |
| 196 | +// TestWorkspaceMetadata_Fields tests metadata fields. |
| 197 | +func TestWorkspaceMetadata_Fields(t *testing.T) { |
| 198 | + t.Parallel() |
| 199 | + |
| 200 | + metadata := &WorkspaceMetadata{ |
| 201 | + TeamID: "T123", |
| 202 | + TeamName: "Test Team", |
| 203 | + BotUserID: "UBOT123", |
| 204 | + } |
| 205 | + |
| 206 | + if metadata.TeamID == "" { |
| 207 | + t.Error("TeamID should not be empty") |
| 208 | + } |
| 209 | + |
| 210 | + if metadata.TeamName == "" { |
| 211 | + t.Error("TeamName should not be empty") |
| 212 | + } |
| 213 | + |
| 214 | + if metadata.BotUserID == "" { |
| 215 | + t.Error("BotUserID should not be empty") |
| 216 | + } |
| 217 | +} |
0 commit comments