|
| 1 | +import { request } from "@octokit/request"; |
| 2 | +import fetchMock, { MockMatcherFunction } from "fetch-mock"; |
| 3 | + |
| 4 | +import { createUnauthenticatedAuth } from "../src/index"; |
| 5 | + |
| 6 | +test("README example", async () => { |
| 7 | + const auth = createUnauthenticatedAuth({ |
| 8 | + reason: |
| 9 | + "Handling an installation.deleted event (The app's access has been revoked)", |
| 10 | + }); |
| 11 | + const authentication = await auth(); |
| 12 | + |
| 13 | + expect(authentication).toEqual({ |
| 14 | + type: "unauthenticated", |
| 15 | + reason: |
| 16 | + "Handling an installation.deleted event (The app's access has been revoked)", |
| 17 | + }); |
| 18 | +}); |
| 19 | + |
| 20 | +test("no reason", async () => { |
| 21 | + try { |
| 22 | + // @ts-ignore |
| 23 | + const auth = createUnauthenticatedAuth(); |
| 24 | + throw new Error("Should not resolve"); |
| 25 | + } catch (error) { |
| 26 | + expect(error.message).toMatch( |
| 27 | + /No reason passed to createUnauthenticatedAuth/i |
| 28 | + ); |
| 29 | + } |
| 30 | +}); |
| 31 | + |
| 32 | +test('auth.hook(request, "GET /repos/octocat/hello-world")', async () => { |
| 33 | + const expectedRequestHeaders = { |
| 34 | + accept: "application/vnd.github.v3+json", |
| 35 | + "user-agent": "test", |
| 36 | + }; |
| 37 | + |
| 38 | + const matchGetUser: MockMatcherFunction = (url, { body, headers }) => { |
| 39 | + expect(url).toEqual("https://api.github.com/repos/octocat/hello-world"); |
| 40 | + expect(headers).toStrictEqual(expectedRequestHeaders); |
| 41 | + return true; |
| 42 | + }; |
| 43 | + |
| 44 | + const requestMock = request.defaults({ |
| 45 | + headers: { |
| 46 | + "user-agent": "test", |
| 47 | + }, |
| 48 | + request: { |
| 49 | + fetch: fetchMock.sandbox().getOnce(matchGetUser, { id: 123 }), |
| 50 | + }, |
| 51 | + }); |
| 52 | + |
| 53 | + const { hook } = createUnauthenticatedAuth({ reason: "test" }); |
| 54 | + const { data } = await hook(requestMock, "GET /repos/octocat/hello-world"); |
| 55 | + |
| 56 | + expect(data).toStrictEqual({ id: 123 }); |
| 57 | +}); |
| 58 | + |
| 59 | +test('auth.hook(request, "GET /repos/octocat/hello-world") returns 404', async () => { |
| 60 | + const expectedRequestHeaders = { |
| 61 | + accept: "application/vnd.github.v3+json", |
| 62 | + "user-agent": "test", |
| 63 | + }; |
| 64 | + |
| 65 | + const matchGetUser: MockMatcherFunction = (url, { body, headers }) => { |
| 66 | + expect(url).toEqual("https://api.github.com/repos/octocat/hello-world"); |
| 67 | + expect(headers).toStrictEqual(expectedRequestHeaders); |
| 68 | + return true; |
| 69 | + }; |
| 70 | + |
| 71 | + const requestMock = request.defaults({ |
| 72 | + headers: { |
| 73 | + "user-agent": "test", |
| 74 | + }, |
| 75 | + request: { |
| 76 | + fetch: fetchMock.sandbox().getOnce(matchGetUser, { |
| 77 | + status: 404, |
| 78 | + body: { |
| 79 | + message: "Not Found", |
| 80 | + }, |
| 81 | + }), |
| 82 | + }, |
| 83 | + }); |
| 84 | + |
| 85 | + const { hook } = createUnauthenticatedAuth({ reason: "test" }); |
| 86 | + |
| 87 | + try { |
| 88 | + await hook(requestMock, "GET /repos/octocat/hello-world"); |
| 89 | + throw new Error("should not resolve"); |
| 90 | + } catch (error) { |
| 91 | + expect(error.message).toBe( |
| 92 | + "Not found. May be due to lack of authentication. Reason: test" |
| 93 | + ); |
| 94 | + } |
| 95 | +}); |
| 96 | + |
| 97 | +test('auth.hook(request, "GET /repos/octocat/hello-world") returns rate limit response', async () => { |
| 98 | + const expectedRequestHeaders = { |
| 99 | + accept: "application/vnd.github.v3+json", |
| 100 | + "user-agent": "test", |
| 101 | + }; |
| 102 | + |
| 103 | + const matchGetUser: MockMatcherFunction = (url, { body, headers }) => { |
| 104 | + expect(url).toEqual("https://api.github.com/repos/octocat/hello-world"); |
| 105 | + expect(headers).toStrictEqual(expectedRequestHeaders); |
| 106 | + return true; |
| 107 | + }; |
| 108 | + |
| 109 | + const requestMock = request.defaults({ |
| 110 | + headers: { |
| 111 | + "user-agent": "test", |
| 112 | + }, |
| 113 | + request: { |
| 114 | + fetch: fetchMock.sandbox().getOnce(matchGetUser, { |
| 115 | + status: 403, |
| 116 | + headers: { |
| 117 | + "x-ratelimit-remaining": 0, |
| 118 | + }, |
| 119 | + body: { |
| 120 | + message: |
| 121 | + "API rate limit exceeded for xxx.xxx.xxx.xxx. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)", |
| 122 | + }, |
| 123 | + }), |
| 124 | + }, |
| 125 | + }); |
| 126 | + |
| 127 | + const { hook } = createUnauthenticatedAuth({ reason: "test" }); |
| 128 | + |
| 129 | + try { |
| 130 | + await hook(requestMock, "GET /repos/octocat/hello-world"); |
| 131 | + throw new Error("should not resolve"); |
| 132 | + } catch (error) { |
| 133 | + expect(error.message).toBe( |
| 134 | + "API rate limit exceeded. This maybe caused by the lack of authentication. Reason: test" |
| 135 | + ); |
| 136 | + } |
| 137 | +}); |
| 138 | + |
| 139 | +test('auth.hook(request, "GET /repos/octocat/hello-world") returns rate limit response', async () => { |
| 140 | + const expectedRequestHeaders = { |
| 141 | + accept: "application/vnd.github.v3+json", |
| 142 | + "user-agent": "test", |
| 143 | + }; |
| 144 | + |
| 145 | + const matchGetUser: MockMatcherFunction = (url, { body, headers }) => { |
| 146 | + expect(url).toEqual("https://api.github.com/repos/octocat/hello-world"); |
| 147 | + expect(headers).toStrictEqual(expectedRequestHeaders); |
| 148 | + return true; |
| 149 | + }; |
| 150 | + |
| 151 | + const requestMock = request.defaults({ |
| 152 | + headers: { |
| 153 | + "user-agent": "test", |
| 154 | + }, |
| 155 | + request: { |
| 156 | + fetch: fetchMock.sandbox().getOnce(matchGetUser, { |
| 157 | + status: 403, |
| 158 | + body: { |
| 159 | + message: |
| 160 | + "You have triggered an abuse detection mechanism and have been temporarily blocked from content creation. Please retry your request again later.", |
| 161 | + }, |
| 162 | + }), |
| 163 | + }, |
| 164 | + }); |
| 165 | + |
| 166 | + const { hook } = createUnauthenticatedAuth({ reason: "test" }); |
| 167 | + |
| 168 | + try { |
| 169 | + await hook(requestMock, "GET /repos/octocat/hello-world"); |
| 170 | + throw new Error("should not resolve"); |
| 171 | + } catch (error) { |
| 172 | + expect(error.message).toBe( |
| 173 | + "You have triggered an abuse detection mechanism. This maybe caused by the lack of authentication. Reason: test" |
| 174 | + ); |
| 175 | + } |
| 176 | +}); |
| 177 | + |
| 178 | +test('auth.hook(request, "PATCH /repos/octocat/hello-world") fails without sending request', async () => { |
| 179 | + const requestMock = request.defaults({ |
| 180 | + headers: { |
| 181 | + "user-agent": "test", |
| 182 | + }, |
| 183 | + request: { |
| 184 | + fetch: fetchMock.sandbox(), |
| 185 | + }, |
| 186 | + }); |
| 187 | + |
| 188 | + const { hook } = createUnauthenticatedAuth({ reason: "test" }); |
| 189 | + |
| 190 | + try { |
| 191 | + await hook(requestMock, "PATCH /repos/octocat/hello-world"); |
| 192 | + throw new Error("should not resolve"); |
| 193 | + } catch (error) { |
| 194 | + expect(error.message).toBe( |
| 195 | + '"PATCH /repos/octocat/hello-world" is not permitted due to lack authentication. Reason: test' |
| 196 | + ); |
| 197 | + } |
| 198 | +}); |
| 199 | + |
| 200 | +test('auth.hook(request, "GET /repos/octocat/hello-world") does not swallow non-request related errors', async () => { |
| 201 | + const requestMock = request.defaults({ |
| 202 | + headers: { |
| 203 | + "user-agent": "test", |
| 204 | + }, |
| 205 | + request: { |
| 206 | + fetch() { |
| 207 | + throw new Error("unrelated"); |
| 208 | + }, |
| 209 | + }, |
| 210 | + }); |
| 211 | + |
| 212 | + const { hook } = createUnauthenticatedAuth({ reason: "test" }); |
| 213 | + |
| 214 | + try { |
| 215 | + await hook(requestMock, "GET /repos/octocat/hello-world"); |
| 216 | + throw new Error("should not resolve"); |
| 217 | + } catch (error) { |
| 218 | + expect(error.message).toBe("unrelated"); |
| 219 | + } |
| 220 | +}); |
0 commit comments