feat: autoset username

This commit is contained in:
Aravinth Manivannan 2023-09-19 03:36:40 +05:30
parent 7fa40b5872
commit 674815ca70
Signed by: realaravinth
GPG key ID: F8F50389936984FF
2 changed files with 7 additions and 11 deletions

View file

@ -9,14 +9,10 @@ const forgeoUrl = authtoken["forgejo_url"];
const token = authtoken["sha1"]; const token = authtoken["sha1"];
const username = authtoken["login"]; const username = authtoken["login"];
const repo = authtoken["repo"]; const repo = authtoken["repo"];
const api = new Forgejo(forgeoUrl, "owner_user"); const api = new Forgejo(forgeoUrl);
api.setTokenAuth(token);
test("use authentication without setting it ", () => { test("use authentication without setting it ", async () => {
const api = new Forgejo(forgeoUrl, "owner_user"); await api.setTokenAuth(token);
expect(() => api.getTokenAuth()).toThrow();
api.setTokenAuth(token);
expect(api.getTokenAuth().getToken()).toBe(token); expect(api.getTokenAuth().getToken()).toBe(token);
let headers = api.getTokenAuthHeader(); let headers = api.getTokenAuthHeader();
expect(headers.Authorization).toBe(`token ${token}`); expect(headers.Authorization).toBe(`token ${token}`);

View file

@ -17,19 +17,19 @@ class Forgejo {
* Represents a Forgejo instance. * Represents a Forgejo instance.
* @constructor * @constructor
* @param {string} url - The URL of the Forgejo instance * @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.url = new URL(url);
this.username = username;
} }
/** /**
* Authenticate with token * Authenticate with token
* @param {Auth} token - access token * @param {Auth} token - access token
*/ */
setTokenAuth(token: string) { async setTokenAuth(token: string) {
this.token = new Auth(token); this.token = new Auth(token);
let res = await this.getUser();
this.username = res.username;
} }
/** /**