Skip to content

Commit bce4311

Browse files
committed
test: assert Authorization header uses GITHUB_TOKEN value in fallback path
1 parent 6d2c799 commit bce4311

2 files changed

Lines changed: 61 additions & 39 deletions

File tree

lib/github.test.ts

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ describe('fetchGitHubContributions', () => {
7878
expect(url).toBe('https://api.github.com/graphql');
7979
expect(options?.method).toBe('POST');
8080

81-
// Make sure the username is wired into the GraphQL variables, not hardcoded.
8281
const body = JSON.parse(options?.body as string);
8382
expect(body.variables).toEqual({ login: 'octocat' });
8483
expect(body.query).toContain('contributionCalendar');
@@ -109,7 +108,6 @@ describe('fetchGitHubContributions', () => {
109108
});
110109

111110
it('throws with the status code when the server returns 401 (expired or missing token)', async () => {
112-
// A 401 is the most common real-world failure — bad or missing GITHUB_PAT.
113111
vi.mocked(fetch).mockResolvedValue(mockResponse({ message: 'Unauthorized' }, 401));
114112

115113
await expect(fetchGitHubContributions('octocat')).rejects.toThrow(
@@ -131,7 +129,6 @@ describe('fetchGitHubContributions', () => {
131129
})
132130
);
133131

134-
// Only the first error surfaces — the source always reads errors[0].
135132
await expect(fetchGitHubContributions('octocat')).rejects.toThrow('Bad credentials');
136133
});
137134

@@ -142,6 +139,41 @@ describe('fetchGitHubContributions', () => {
142139
'GitHub user "ghost-user-xyz" not found'
143140
);
144141
});
142+
143+
// NEW TEST — GITHUB_TOKEN fallback header assertion
144+
it('uses GITHUB_TOKEN value in Authorization header when GITHUB_PAT is not set', async () => {
145+
const originalPat = process.env.GITHUB_PAT;
146+
const originalToken = process.env.GITHUB_TOKEN;
147+
148+
delete process.env.GITHUB_PAT;
149+
process.env.GITHUB_TOKEN = 'my-actions-token';
150+
151+
vi.mocked(fetch).mockResolvedValue(
152+
mockResponse({
153+
data: {
154+
user: {
155+
contributionsCollection: {
156+
contributionCalendar: mockCalendar,
157+
},
158+
},
159+
},
160+
})
161+
);
162+
163+
await fetchGitHubContributions('octocat');
164+
165+
const [, options] = vi.mocked(fetch).mock.calls[0];
166+
expect(options?.headers).toMatchObject({
167+
Authorization: 'bearer my-actions-token',
168+
});
169+
170+
// Restore env
171+
if (originalPat === undefined) delete process.env.GITHUB_PAT;
172+
else process.env.GITHUB_PAT = originalPat;
173+
174+
if (originalToken === undefined) delete process.env.GITHUB_TOKEN;
175+
else process.env.GITHUB_TOKEN = originalToken;
176+
});
145177
});
146178

147179
describe('fetchUserProfile', () => {
@@ -209,7 +241,6 @@ describe('getFullDashboardData', () => {
209241
location: 'Earth',
210242
});
211243
}
212-
// GraphQL
213244
return mockResponse({
214245
data: {
215246
user: { contributionsCollection: { contributionCalendar: mockCalendar } },
@@ -322,4 +353,4 @@ describe('GitHub API cache behavior', () => {
322353

323354
expect(fetch).toHaveBeenCalledTimes(2);
324355
});
325-
});
356+
});

package-lock.json

Lines changed: 25 additions & 34 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)