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,37 +46,43 @@ 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
.getAppLinkAsync(getAppLinkMessage())
.then(
([result]: [GetAppLinkResult]) =>
result.getAppLinkResult.authorizeAccount.deviceLink
)
.then(({ regUrl, linkCode }: { regUrl: string; linkCode: string }) => ({
login: async ({ username, password }: Credentials) => {
await request(this.server).get(this.stripServiceRoot(regUrl)).expect(200);
return request(this.server) return client
.post(this.stripServiceRoot(regUrl)) .getAppLinkAsync(getAppLinkMessage())
.type("form") .then(
.send({ username, password, linkCode }) ([result]: [GetAppLinkResult]) =>
.then(response => ({ result.getAppLinkResult.authorizeAccount.deviceLink
expectSuccess: () => { )
expect(response.status).toEqual(200); .then(({ regUrl, linkCode }: { regUrl: string; linkCode: string }) => ({
expect(response.text).toContain("Login successful") login: async ({ username, password }: Credentials) => {
}, await request(this.server)
expectFailure: () => { .get(this.stripServiceRoot(regUrl))
expect(response.status).toEqual(403); .expect(200);
expect(response.text).toContain("Login failed")
}, return request(this.server)
})); .post(this.stripServiceRoot(regUrl))
}, .type("form")
})) .send({ username, password, linkCode })
); .then((response) => ({
expectSuccess: async () => {
expect(response.status).toEqual(200);
expect(response.text).toContain("Login successful");
return client
.getDeviceAuthTokenAsync({ linkCode })
.then((authToken: [GetDeviceAuthTokenResult, any]) => new FooDriver(client, authToken[0]));
},
expectFailure: () => {
expect(response.status).toEqual(403);
expect(response.text).toContain("Login failed");
},
}));
},
}));
} }
} }