Skip to content

fix(#2): inject ITableauTokenProvider into WorkbookService#20

Merged
teesofttech merged 1 commit into
masterfrom
fix/issue-2-workbook-service-token
Jun 12, 2026
Merged

fix(#2): inject ITableauTokenProvider into WorkbookService#20
teesofttech merged 1 commit into
masterfrom
fix/issue-2-workbook-service-token

Conversation

@teesofttech

@teesofttech teesofttech commented Jun 12, 2026

Copy link
Copy Markdown
Owner

Problem

Closes #2

WorkbookService declared private readonly string _token which was never assigned. Every method called CreateClient(_token) with null, so the X-Tableau-Auth header was always empty — every workbook API call returned 401 Unauthorized.

Root cause

The service was written without ITableauTokenProvider injection, unlike ViewService, DataSourceService, and PermissionService which already use it correctly.

Changes

WorkbookService.cs

  • Remove the dead private readonly string _token field
  • Inject ITableauTokenProvider via constructor
  • Replace CreateClient(string token) with parameterless CreateClient() that calls _tokenProvider.GetToken() at request time — matches the pattern in ViewService

WorkbookServiceTests.cs (new — 10 tests)

  • Auth token is read from ITableauTokenProvider on every call (GetAll, GetById, Delete)
  • Correct X-Tableau-Auth header value is sent in each request
  • 401 from the API surfaces as HttpRequestException
  • InvalidOperationException from an unset token provider propagates correctly
  • 404 from the API surfaces as HttpRequestException

Test results

Total tests: 11 | Passed: 11 | Failed: 0

Note: This PR is based on infra/phase-0-test-examples-pipeline (#19) which adds Moq and RichardSzalay.MockHttp to the test project. Merge #19 first, or the test project diff will include the infra changes.

🤖 Generated with Claude Code

Summary by CodeRabbit

Release Notes

  • Refactor

    • Updated authentication token handling in WorkbookService to use dynamic token provider injection for improved flexibility.
  • Tests

    • Added comprehensive test suite for WorkbookService covering workbook retrieval, deletion, and authentication scenarios.

WorkbookService previously declared `private readonly string _token`
which was never assigned, causing every Tableau API call (GetAll,
GetById, Publish, Delete) to send a null X-Tableau-Auth header and
receive 401 Unauthorized.

- Inject ITableauTokenProvider via constructor (matches ViewService,
  DataSourceService, PermissionService pattern)
- Replace CreateClient(string token) with parameterless CreateClient()
  that calls _tokenProvider.GetToken() at request time
- Remove the dead _token field

Tests: 10 new unit tests in WorkbookServiceTests verify
- Auth token is read from ITableauTokenProvider on each call
- Correct X-Tableau-Auth header value is sent
- 401 from the API surfaces as HttpRequestException
- InvalidOperationException from an unset token propagates correctly

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings June 12, 2026 08:57
@coderabbitai

coderabbitai Bot commented Jun 12, 2026

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

Pull request was closed or merged during review

📝 Walkthrough

Walkthrough

WorkbookService is refactored to inject ITableauTokenProvider and retrieve auth tokens dynamically on each client creation instead of storing a null token field. All HTTP request methods are updated to use the new parameterless CreateClient() method. A comprehensive test suite validates the refactoring with mocked HTTP responses and token provider interactions.

Changes

Token Provider Injection and Testing

Layer / File(s) Summary
Refactor WorkbookService to use ITableauTokenProvider
src/TableauSharp/Workbooks/Services/WorkbookService.cs
Constructor accepts ITableauTokenProvider injection; CreateClient() is changed from CreateClient(string token) to a parameterless method that calls _tokenProvider.GetToken() to set the X-Tableau-Auth header. All request methods (GetAllAsync, GetByIdAsync, PublishAsync, DeleteAsync) are updated to call CreateClient() without passing a token.
Add WorkbookService test suite with mock HTTP responses
test/TableauSharp.Tests/Workbooks/WorkbookServiceTests.cs
NUnit test class sets up strict mocks for IHttpClientFactory and ITableauTokenProvider, wires MockHttp to return canned JSON payloads, and provides test cases for GetAllAsync, GetByIdAsync, and DeleteAsync that verify auth header injection, token provider invocation, expected HTTP status handling (401, 404, 204), and correct response deserialization.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

A token once lost in the null, now found,
Through provider's injection, safe and sound.
Tests mock the API with responses true,
WorkbookService shines with headers anew. 📚✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: injecting ITableauTokenProvider into WorkbookService to fix the token-handling issue.
Linked Issues check ✅ Passed The pull request fully addresses issue #2 by injecting ITableauTokenProvider, removing the unused _token field, and updating CreateClient() to call _tokenProvider.GetToken() at request time, matching the pattern used by other services.
Out of Scope Changes check ✅ Passed All changes are directly related to fixing the token provider injection in WorkbookService and include appropriate test coverage for the modifications.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/issue-2-workbook-service-token

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@teesofttech
teesofttech merged commit 43f331f into master Jun 12, 2026
2 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: WorkbookService._token is always null causing 401 on all workbook calls

2 participants