lokalise_auth.spec.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. require("../setup");
  2. import { expect } from "chai";
  3. import { Cassettes } from "mocha-cassettes";
  4. import {
  5. RequestTokenResponse,
  6. RefreshTokenResponse,
  7. AuthError,
  8. LokaliseAuth,
  9. } from "../../src/main";
  10. describe("LokaliseAuth", function () {
  11. const cassette = new Cassettes("./test/cassettes");
  12. const lokaliseAuth = new LokaliseAuth("123abc", "456zyx");
  13. describe("constructor", function () {
  14. it("is expected to throw an error if the client secret is not provided", function () {
  15. expect(function () {
  16. new LokaliseAuth("fake", "");
  17. }).to.throw(Error);
  18. });
  19. it("is expected to throw an error if the client id is not provided", function () {
  20. expect(function () {
  21. new LokaliseAuth("", "fake");
  22. }).to.throw(Error);
  23. });
  24. it("is expected to set authData", function () {
  25. const dummy = new LokaliseAuth("id", "secret");
  26. expect(dummy.authData.client_id).to.eq("id");
  27. expect(dummy.authData.client_secret).to.eq("secret");
  28. });
  29. });
  30. describe("auth", function () {
  31. it("returns proper url with all set attrs", function () {
  32. const url = lokaliseAuth.auth(
  33. ["read_projects", "write_team_groups"],
  34. "http://example.com/redirect",
  35. "random123"
  36. );
  37. expect(url).to.include("?client_id=123abc");
  38. expect(url).to.include("&scope=read_projects+write_team_groups");
  39. expect(url).to.include("&state=random123");
  40. expect(url).to.include(
  41. "&redirect_uri=http%3A%2F%2Fexample.com%2Fredirect"
  42. );
  43. });
  44. it("returns proper url with scope as string", function () {
  45. const url = lokaliseAuth.auth("read_projects");
  46. expect(url).to.include("?client_id=123abc");
  47. expect(url).to.include("&scope=read_projects");
  48. });
  49. });
  50. describe("token", function () {
  51. cassette
  52. .createTest("valid code", async () => {
  53. const resp: RequestTokenResponse = await lokaliseAuth.token(
  54. "fdf9876214cffa10c6ebbb01bd2399077ba9c49e"
  55. );
  56. expect(resp.access_token).to.eq("stubbed access");
  57. expect(resp.refresh_token).to.eq("stubbed refresh");
  58. expect(resp.expires_in).to.eq(3600);
  59. expect(resp.token_type).to.eq("Bearer");
  60. })
  61. .register(this);
  62. cassette
  63. .createTest("error", async () => {
  64. await lokaliseAuth.token("fake").catch((e: AuthError) => {
  65. expect(e.code).to.equal(400);
  66. expect(e.error).to.equal("invalid_request");
  67. expect(e.error_description).to.equal(
  68. "client_id: Client id must be 40 characters long"
  69. );
  70. expect(e.error_uri).to.equal("");
  71. });
  72. })
  73. .register(this);
  74. });
  75. describe("refresh", function () {
  76. cassette
  77. .createTest("valid token", async () => {
  78. const resp: RefreshTokenResponse = await lokaliseAuth.refresh(
  79. "stubbed refresh"
  80. );
  81. expect(resp.access_token).to.eq("stubbed access");
  82. expect(resp.scope).to.eq("write_team_groups read_projects");
  83. expect(resp.expires_in).to.eq(3600);
  84. expect(resp.token_type).to.eq("Bearer");
  85. })
  86. .register(this);
  87. cassette
  88. .createTest("error", async () => {
  89. await lokaliseAuth.refresh("fake").catch((e: AuthError) => {
  90. expect(e.code).to.equal(400);
  91. expect(e.error).to.equal("invalid_request");
  92. expect(e.error_description).to.equal(
  93. "client_id: Client id must be 40 characters long"
  94. );
  95. expect(e.error_uri).to.equal("");
  96. });
  97. })
  98. .register(this);
  99. });
  100. });