updates to support IndieAuth spec 20220212 metadata and issuer
[squeep-authentication-module] / test / lib / session-manager.js
index daff0501e3f0cab2cd8a00a414cf01c4a30ab303..bf8df03b94f8875bfa5306e6f9a59f51546cfbe6 100644 (file)
@@ -84,7 +84,9 @@ describe('SessionManager', function () {
     it('covers valid profile', async function () {
       ctx.parsedBody.me = 'https://example.com/profile';
       manager.indieAuthCommunication.fetchProfile.resolves({
-        authorizationEndpoint: 'https://example.com/auth',
+        metadata: {
+          authorizationEndpoint: 'https://example.com/auth',
+        },
       });
       await manager.postAdminLogin(res, ctx);
       assert.strictEqual(res.statusCode, 302);
@@ -104,11 +106,48 @@ describe('SessionManager', function () {
     it('covers invalid profile response endpoint', async function () {
       ctx.parsedBody.me = 'https://example.com/profile';
       manager.indieAuthCommunication.fetchProfile.resolves({
-        authorizationEndpoint: 'not an auth endpoint',
+        metadata: {
+          authorizationEndpoint: 'not an auth endpoint',
+        },
       });
       await manager.postAdminLogin(res, ctx);
       assert(!res.setHeader.called);
     });
+    describe('living-standard-20220212', function () {
+      it('covers valid profile', async function () {
+        ctx.parsedBody.me = 'https://example.com/profile';
+        manager.indieAuthCommunication.fetchProfile.resolves({
+          metadata: {
+            issuer: 'https://example.com/',
+            authorizationEndpoint: 'https://example.com/auth',
+          },
+        });
+        await manager.postAdminLogin(res, ctx);
+        assert.strictEqual(res.statusCode, 302);
+      });
+      it('covers bad issuer url', async function () {
+        ctx.parsedBody.me = 'https://example.com/profile';
+        manager.indieAuthCommunication.fetchProfile.resolves({
+          metadata: {
+            issuer: 'http://example.com/?bah#foo',
+            authorizationEndpoint: 'https://example.com/auth',
+          },
+        });
+        await manager.postAdminLogin(res, ctx);
+        assert(!res.setHeader.called);
+      });
+      it('covers unparsable issuer url', async function () {
+        ctx.parsedBody.me = 'https://example.com/profile';
+        manager.indieAuthCommunication.fetchProfile.resolves({
+          metadata: {
+            issuer: 'not a url',
+            authorizationEndpoint: 'https://example.com/auth',
+          },
+        });
+        await manager.postAdminLogin(res, ctx);
+        assert(!res.setHeader.called);
+      });
+    }); // living-standard-20220212
   }); // postAdminLogin
 
   describe('getAdminLogout', function () {
@@ -128,7 +167,9 @@ describe('SessionManager', function () {
         me,
       });
       manager.indieAuthCommunication.fetchProfile.resolves({
-        authorizationEndpoint,
+        metadata: {
+          authorizationEndpoint,
+        },
       });
       sinon.stub(manager.mysteryBox, 'unpack').resolves({
         authorizationEndpoint,
@@ -242,13 +283,48 @@ describe('SessionManager', function () {
       });
       manager.indieAuthCommunication.fetchProfile.restore();
       sinon.stub(manager.indieAuthCommunication, 'fetchProfile').resolves({
-        authorizationEndpoint: 'https://elsewhere.example.com/auth',
+        metadata: {
+          authorizationEndpoint: 'https://elsewhere.example.com/auth',
+        },
       });
 
       await manager.getAdminIA(res, ctx);
 
       assert(ctx.errors.length);
     });
+    describe('living-standard-20220212', function () {
+      beforeEach(function () {
+        manager.indieAuthCommunication.fetchProfile.resolves({
+          metadata: {
+            authorizationEndpoint,
+            issuer: 'https://example.com/',
+          },
+        });
+        manager.mysteryBox.unpack.resolves({
+          authorizationEndpoint,
+          issuer: 'https://example.com/',
+          state,
+          me,
+        });
+      });
+      it('covers valid', async function () {
+        ctx.queryParams['state'] = state;
+        ctx.queryParams['code'] = 'codeCodeCode';
+        ctx.queryParams['iss'] = 'https://example.com/';
+
+        await manager.getAdminIA(res, ctx);
+
+        assert.strictEqual(res.statusCode, 302);
+      });
+      it('covers mis-matched issuer', async function () {
+        ctx.queryParams['state'] = state;
+        ctx.queryParams['code'] = 'codeCodeCode';
+
+        await manager.getAdminIA(res, ctx);
+
+        assert(ctx.errors.length);
+      });
+    }); // living-standard-20220212
   }); // getAdminIA
 
 }); // SessionManager
\ No newline at end of file