Skip to content

Commit d459393

Browse files
authored
Makes sure we plumb auth.installationId when updating installations (#2739)
1 parent 2183b84 commit d459393

File tree

2 files changed

+60
-22
lines changed

2 files changed

+60
-22
lines changed

spec/ParseInstallation.spec.js

+39-5
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ describe('Installations', () => {
3737
expect(obj.installationId).toEqual(installId);
3838
expect(obj.deviceType).toEqual(device);
3939
done();
40-
}).catch((error) => { console.log(error); });
40+
}).catch((error) => { console.log(error); jfail(error); done(); });
4141
});
4242

4343
it('creates an ios installation with ids', (done) => {
@@ -55,7 +55,7 @@ describe('Installations', () => {
5555
expect(obj.deviceToken).toEqual(t);
5656
expect(obj.deviceType).toEqual(device);
5757
done();
58-
}).catch((error) => { console.log(error); });
58+
}).catch((error) => { console.log(error); jfail(error); done(); });
5959
});
6060

6161
it('creates an embedded installation with ids', (done) => {
@@ -73,7 +73,7 @@ describe('Installations', () => {
7373
expect(obj.installationId).toEqual(installId);
7474
expect(obj.deviceType).toEqual(device);
7575
done();
76-
}).catch((error) => { console.log(error); });
76+
}).catch((error) => { console.log(error); jfail(error); done(); });
7777
});
7878

7979
it('creates an android installation with all fields', (done) => {
@@ -96,7 +96,7 @@ describe('Installations', () => {
9696
expect(obj.channels[0]).toEqual('foo');
9797
expect(obj.channels[1]).toEqual('bar');
9898
done();
99-
}).catch((error) => { console.log(error); });
99+
}).catch((error) => { console.log(error); jfail(error); done(); });
100100
});
101101

102102
it('creates an ios installation with all fields', (done) => {
@@ -119,7 +119,7 @@ describe('Installations', () => {
119119
expect(obj.channels[0]).toEqual('foo');
120120
expect(obj.channels[1]).toEqual('bar');
121121
done();
122-
}).catch((error) => { console.log(error); });
122+
}).catch((error) => { console.log(error); jfail(error); done(); });
123123
});
124124

125125
it('should properly fail queying installations', (done) => {
@@ -872,6 +872,40 @@ describe('Installations', () => {
872872
});
873873
});
874874

875+
it('allows you to update installation from header (#2090)', done => {
876+
let installId = '12345678-abcd-abcd-abcd-123456789abc';
877+
let device = 'android';
878+
let input = {
879+
'installationId': installId,
880+
'deviceType': device
881+
};
882+
rest.create(config, auth.nobody(config), '_Installation', input)
883+
.then(createResult => {
884+
let headers = {
885+
'X-Parse-Application-Id': 'test',
886+
'X-Parse-REST-API-Key': 'rest',
887+
'X-Parse-Installation-Id': installId
888+
};
889+
request.post({
890+
headers: headers,
891+
url: 'http://localhost:8378/1/classes/_Installation',
892+
json: true,
893+
body: {
894+
date: new Date()
895+
}
896+
}, (error, response, body) => {
897+
expect(response.statusCode).toBe(200);
898+
expect(body.updatedAt).not.toBeUndefined();
899+
done();
900+
});
901+
})
902+
.catch(error => {
903+
console.log(error);
904+
fail('failed');
905+
done();
906+
});
907+
});
908+
875909
// TODO: Look at additional tests from installation_collection_test.go:882
876910
// TODO: Do we need to support _tombstone disabling of installations?
877911
// TODO: Test deletion, badge increments

src/RestWrite.js

+21-17
Original file line numberDiff line numberDiff line change
@@ -561,31 +561,30 @@ RestWrite.prototype.handleInstallation = function() {
561561
return;
562562
}
563563

564-
if (!this.query && !this.data.deviceToken && !this.data.installationId) {
564+
if (!this.query && !this.data.deviceToken && !this.data.installationId && !this.auth.installationId) {
565565
throw new Parse.Error(135,
566566
'at least one ID field (deviceToken, installationId) ' +
567567
'must be specified in this operation');
568568
}
569569

570-
if (!this.query && !this.data.deviceType) {
571-
throw new Parse.Error(135,
572-
'deviceType must be specified in this operation');
573-
}
574-
575570
// If the device token is 64 characters long, we assume it is for iOS
576571
// and lowercase it.
577572
if (this.data.deviceToken && this.data.deviceToken.length == 64) {
578573
this.data.deviceToken = this.data.deviceToken.toLowerCase();
579574
}
580575

581-
// TODO: We may need installationId from headers, plumb through Auth?
582-
// per installation_handler.go
583-
584576
// We lowercase the installationId if present
585577
if (this.data.installationId) {
586578
this.data.installationId = this.data.installationId.toLowerCase();
587579
}
588580

581+
// If data.installationId is not set, we can lookup in the auth
582+
let installationId = this.data.installationId || this.auth.installationId;
583+
584+
if (installationId) {
585+
installationId = installationId.toLowerCase();
586+
}
587+
589588
var promise = Promise.resolve();
590589

591590
var idMatch; // Will be a match on either objectId or installationId
@@ -600,9 +599,9 @@ RestWrite.prototype.handleInstallation = function() {
600599
objectId: this.query.objectId
601600
});
602601
}
603-
if (this.data.installationId) {
602+
if (installationId) {
604603
orQueries.push({
605-
'installationId': this.data.installationId
604+
'installationId': installationId
606605
});
607606
}
608607
if (this.data.deviceToken) {
@@ -622,7 +621,7 @@ RestWrite.prototype.handleInstallation = function() {
622621
if (this.query && this.query.objectId && result.objectId == this.query.objectId) {
623622
objectIdMatch = result;
624623
}
625-
if (result.installationId == this.data.installationId) {
624+
if (result.installationId == installationId) {
626625
installationIdMatch = result;
627626
}
628627
if (result.deviceToken == this.data.deviceToken) {
@@ -636,8 +635,8 @@ RestWrite.prototype.handleInstallation = function() {
636635
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND,
637636
'Object not found for update.');
638637
}
639-
if (this.data.installationId && objectIdMatch.installationId &&
640-
this.data.installationId !== objectIdMatch.installationId) {
638+
if (installationId && objectIdMatch.installationId &&
639+
installationId !== objectIdMatch.installationId) {
641640
throw new Parse.Error(136,
642641
'installationId may not be changed in this ' +
643642
'operation');
@@ -661,16 +660,21 @@ RestWrite.prototype.handleInstallation = function() {
661660
idMatch = objectIdMatch;
662661
}
663662

664-
if (this.data.installationId && installationIdMatch) {
663+
if (installationId && installationIdMatch) {
665664
idMatch = installationIdMatch;
666665
}
666+
// need to specify deviceType only if it's new
667+
if (!this.query && !this.data.deviceType && !idMatch) {
668+
throw new Parse.Error(135,
669+
'deviceType must be specified in this operation');
670+
}
667671

668672
}).then(() => {
669673
if (!idMatch) {
670674
if (!deviceTokenMatches.length) {
671675
return;
672676
} else if (deviceTokenMatches.length == 1 &&
673-
(!deviceTokenMatches[0]['installationId'] || !this.data.installationId)
677+
(!deviceTokenMatches[0]['installationId'] || !installationId)
674678
) {
675679
// Single match on device token but none on installationId, and either
676680
// the passed object or the match is missing an installationId, so we
@@ -689,7 +693,7 @@ RestWrite.prototype.handleInstallation = function() {
689693
var delQuery = {
690694
'deviceToken': this.data.deviceToken,
691695
'installationId': {
692-
'$ne': this.data.installationId
696+
'$ne': installationId
693697
}
694698
};
695699
if (this.data.appIdentifier) {

0 commit comments

Comments
 (0)