diff --git a/src/api.test.ts b/src/api.test.ts index 755e05c..8e38781 100644 --- a/src/api.test.ts +++ b/src/api.test.ts @@ -9,14 +9,10 @@ const forgeoUrl = authtoken["forgejo_url"]; const token = authtoken["sha1"]; const username = authtoken["login"]; const repo = authtoken["repo"]; -const api = new Forgejo(forgeoUrl, "owner_user"); -api.setTokenAuth(token); +const api = new Forgejo(forgeoUrl); -test("use authentication without setting it ", () => { - const api = new Forgejo(forgeoUrl, "owner_user"); - - expect(() => api.getTokenAuth()).toThrow(); - api.setTokenAuth(token); +test("use authentication without setting it ", async () => { + await api.setTokenAuth(token); expect(api.getTokenAuth().getToken()).toBe(token); let headers = api.getTokenAuthHeader(); expect(headers.Authorization).toBe(`token ${token}`); diff --git a/src/index.ts b/src/index.ts index d2307d1..457802a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -17,19 +17,19 @@ class Forgejo { * Represents a Forgejo instance. * @constructor * @param {string} url - The URL of the Forgejo instance - * @param {string} username - username of the logged in user */ - constructor(url: string, username: string) { + constructor(url: string) { this.url = new URL(url); - this.username = username; } /** * Authenticate with token * @param {Auth} token - access token */ - setTokenAuth(token: string) { + async setTokenAuth(token: string) { this.token = new Auth(token); + let res = await this.getUser(); + this.username = res.username; } /**