Scenario test fetching auth token

This commit is contained in:
simojenki
2021-02-26 12:31:46 +11:00
parent 12fe09dd7f
commit e3eec6e93e

View File

@@ -1,9 +1,9 @@
import { createClientAsync } from "soap"; import { createClientAsync, Client } from "soap";
import { Express } from "express"; import { Express } from "express";
import request from "supertest"; import request from "supertest";
import { GetAppLinkResult } from "../src/smapi"; import { GetAppLinkResult, GetDeviceAuthTokenResult } from "../src/smapi";
import { getAppLinkMessage } from "./builders"; import { getAppLinkMessage } from "./builders";
import { InMemoryMusicService } from "./in_memory_music_service"; import { InMemoryMusicService } from "./in_memory_music_service";
import { InMemoryLinkCodes } from "../src/link_codes"; import { InMemoryLinkCodes } from "../src/link_codes";
@@ -12,6 +12,16 @@ import makeServer from "../src/server";
import { Service, bonobService, SONOS_DISABLED } from "../src/sonos"; import { Service, bonobService, SONOS_DISABLED } from "../src/sonos";
import supersoap from "./supersoap"; import supersoap from "./supersoap";
class FooDriver {
client: Client;
token: GetDeviceAuthTokenResult;
constructor(client: Client, token: GetDeviceAuthTokenResult) {
this.client = client;
this.token = token;
}
}
class SonosDriver { class SonosDriver {
server: Express; server: Express;
rootUrl: string; rootUrl: string;
@@ -36,11 +46,12 @@ class SonosDriver {
.get(this.stripServiceRoot(this.service.presentation.uri!)) .get(this.stripServiceRoot(this.service.presentation.uri!))
.expect(200); .expect(200);
return createClientAsync(`${this.service.uri}?wsdl`, { const client = await createClientAsync(`${this.service.uri}?wsdl`, {
endpoint: this.service.uri, endpoint: this.service.uri,
httpClient: supersoap(this.server, this.rootUrl), httpClient: supersoap(this.server, this.rootUrl),
}).then((client) => });
client
return client
.getAppLinkAsync(getAppLinkMessage()) .getAppLinkAsync(getAppLinkMessage())
.then( .then(
([result]: [GetAppLinkResult]) => ([result]: [GetAppLinkResult]) =>
@@ -48,25 +59,30 @@ class SonosDriver {
) )
.then(({ regUrl, linkCode }: { regUrl: string; linkCode: string }) => ({ .then(({ regUrl, linkCode }: { regUrl: string; linkCode: string }) => ({
login: async ({ username, password }: Credentials) => { login: async ({ username, password }: Credentials) => {
await request(this.server).get(this.stripServiceRoot(regUrl)).expect(200); await request(this.server)
.get(this.stripServiceRoot(regUrl))
.expect(200);
return request(this.server) return request(this.server)
.post(this.stripServiceRoot(regUrl)) .post(this.stripServiceRoot(regUrl))
.type("form") .type("form")
.send({ username, password, linkCode }) .send({ username, password, linkCode })
.then(response => ({ .then((response) => ({
expectSuccess: () => { expectSuccess: async () => {
expect(response.status).toEqual(200); expect(response.status).toEqual(200);
expect(response.text).toContain("Login successful") expect(response.text).toContain("Login successful");
return client
.getDeviceAuthTokenAsync({ linkCode })
.then((authToken: [GetDeviceAuthTokenResult, any]) => new FooDriver(client, authToken[0]));
}, },
expectFailure: () => { expectFailure: () => {
expect(response.status).toEqual(403); expect(response.status).toEqual(403);
expect(response.text).toContain("Login failed") expect(response.text).toContain("Login failed");
}, },
})); }));
}, },
})) }));
);
} }
} }