Skip to content

Commit a3b9b26

Browse files
author
Lee Richmond
committed
allow usage without jwt
1 parent dc2743f commit a3b9b26

File tree

3 files changed

+52
-5
lines changed

3 files changed

+52
-5
lines changed

src/model.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,22 @@ export default class Model {
4848
}
4949

5050
static getJWT() : string {
51-
return this.getJWTOwner().jwt;
51+
let owner = this.getJWTOwner();
52+
53+
if (owner) {
54+
return owner.jwt;
55+
}
5256
}
5357

5458
static getJWTOwner() : typeof Model {
5559
if (this.isJWTOwner) {
5660
return this;
5761
} else {
58-
return this.parentClass.getJWTOwner();
62+
if (this.parentClass) {
63+
return this.parentClass.getJWTOwner();
64+
} else {
65+
return;
66+
}
5967
}
6068
}
6169

test/fixtures.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,24 @@ class MultiWord extends ApplicationRecord {
6262
const TestJWTSubclass = ApplicationRecord.extend({
6363
});
6464

65-
Config.setup({ jwtOwners: [ApplicationRecord, TestJWTSubclass] });
65+
const NonJWTOwner = Model.extend({
66+
});
67+
68+
Config.setup({
69+
jwtOwners: [
70+
ApplicationRecord,
71+
TestJWTSubclass
72+
]
73+
});
6674

67-
export { ApplicationRecord, TestJWTSubclass, Author, Person, Book, Genre, Bio, Tag };
75+
export {
76+
ApplicationRecord,
77+
TestJWTSubclass,
78+
NonJWTOwner,
79+
Author,
80+
Person,
81+
Book,
82+
Genre,
83+
Bio,
84+
Tag
85+
};

test/unit/model-test.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
import { expect, sinon } from '../test-helper';
22
import { Model } from '../../src/index';
3-
import { ApplicationRecord, TestJWTSubclass, Person, Author, Book, Genre, Bio } from '../fixtures';
3+
import {
4+
ApplicationRecord,
5+
TestJWTSubclass,
6+
NonJWTOwner,
7+
Person,
8+
Author,
9+
Book,
10+
Genre,
11+
Bio
12+
} from '../fixtures';
413

514
let instance;
615

@@ -12,6 +21,12 @@ describe('Model', function() {
1221
expect(ApplicationRecord.getJWTOwner()).to.eq(ApplicationRecord);
1322
expect(TestJWTSubclass.getJWTOwner()).to.eq(TestJWTSubclass);
1423
});
24+
25+
describe('when no owner', function() {
26+
it('returns null', function() {
27+
expect(NonJWTOwner.getJWTOwner()).to.eq(undefined)
28+
});
29+
});
1530
});
1631

1732
describe('#getJWT', function() {
@@ -26,6 +41,12 @@ describe('Model', function() {
2641
it('it grabs jwt from top-most parent', function() {
2742
expect(Author.getJWT()).to.eq('g3tm3');
2843
});
44+
45+
describe('when no JWT owner', function() {
46+
it('returns null', function() {
47+
expect(NonJWTOwner.getJWT()).to.eq(undefined);
48+
});
49+
});
2950
});
3051

3152
describe('#setJWT', function() {

0 commit comments

Comments
 (0)