|
| 1 | +var request = require("request"); |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +function createProduct() { |
| 6 | + const file = new Parse.File("name", { |
| 7 | + base64: new Buffer("download_file", "utf-8").toString("base64") |
| 8 | + }, "text"); |
| 9 | + return file.save().then(function(){ |
| 10 | + var product = new Parse.Object("_Product"); |
| 11 | + product.set({ |
| 12 | + download: file, |
| 13 | + icon: file, |
| 14 | + title: "a product", |
| 15 | + subtitle: "a product", |
| 16 | + order: 1, |
| 17 | + productIdentifier: "a-product" |
| 18 | + }) |
| 19 | + return product.save(); |
| 20 | + }) |
| 21 | + |
| 22 | +} |
| 23 | + |
| 24 | + |
| 25 | +describe("test validate_receipt endpoint", () => { |
| 26 | + |
| 27 | + beforeEach( done => { |
| 28 | + createProduct().then(done).fail(function(err){ |
| 29 | + console.error(err); |
| 30 | + done(); |
| 31 | + }) |
| 32 | + }) |
| 33 | + |
| 34 | + it("should bypass appstore validation", (done) => { |
| 35 | + |
| 36 | + request.post({ |
| 37 | + headers: { |
| 38 | + 'X-Parse-Application-Id': 'test', |
| 39 | + 'X-Parse-REST-API-Key': 'rest'}, |
| 40 | + url: 'http://localhost:8378/1/validate_purchase', |
| 41 | + json: true, |
| 42 | + body: { |
| 43 | + productIdentifier: "a-product", |
| 44 | + receipt: { |
| 45 | + __type: "Bytes", |
| 46 | + base64: new Buffer("receipt", "utf-8").toString("base64") |
| 47 | + }, |
| 48 | + bypassAppStoreValidation: true |
| 49 | + } |
| 50 | + }, function(err, res, body){ |
| 51 | + if (typeof body != "object") { |
| 52 | + fail("Body is not an object"); |
| 53 | + done(); |
| 54 | + } else { |
| 55 | + expect(body.__type).toEqual("File"); |
| 56 | + const url = body.url; |
| 57 | + request.get({ |
| 58 | + url: url |
| 59 | + }, function(err, res, body) { |
| 60 | + expect(body).toEqual("download_file"); |
| 61 | + done(); |
| 62 | + }); |
| 63 | + } |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + it("should fail for missing receipt", (done) => { |
| 68 | + request.post({ |
| 69 | + headers: { |
| 70 | + 'X-Parse-Application-Id': 'test', |
| 71 | + 'X-Parse-REST-API-Key': 'rest'}, |
| 72 | + url: 'http://localhost:8378/1/validate_purchase', |
| 73 | + json: true, |
| 74 | + body: { |
| 75 | + productIdentifier: "a-product", |
| 76 | + bypassAppStoreValidation: true |
| 77 | + } |
| 78 | + }, function(err, res, body){ |
| 79 | + if (typeof body != "object") { |
| 80 | + fail("Body is not an object"); |
| 81 | + done(); |
| 82 | + } else { |
| 83 | + expect(body.code).toEqual(Parse.Error.INVALID_JSON); |
| 84 | + done(); |
| 85 | + } |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + it("should fail for missing product identifier", (done) => { |
| 90 | + request.post({ |
| 91 | + headers: { |
| 92 | + 'X-Parse-Application-Id': 'test', |
| 93 | + 'X-Parse-REST-API-Key': 'rest'}, |
| 94 | + url: 'http://localhost:8378/1/validate_purchase', |
| 95 | + json: true, |
| 96 | + body: { |
| 97 | + receipt: { |
| 98 | + __type: "Bytes", |
| 99 | + base64: new Buffer("receipt", "utf-8").toString("base64") |
| 100 | + }, |
| 101 | + bypassAppStoreValidation: true |
| 102 | + } |
| 103 | + }, function(err, res, body){ |
| 104 | + if (typeof body != "object") { |
| 105 | + fail("Body is not an object"); |
| 106 | + done(); |
| 107 | + } else { |
| 108 | + expect(body.code).toEqual(Parse.Error.INVALID_JSON); |
| 109 | + done(); |
| 110 | + } |
| 111 | + }); |
| 112 | + }); |
| 113 | + |
| 114 | + it("should bypass appstore validation and not find product", (done) => { |
| 115 | + |
| 116 | + request.post({ |
| 117 | + headers: { |
| 118 | + 'X-Parse-Application-Id': 'test', |
| 119 | + 'X-Parse-REST-API-Key': 'rest'}, |
| 120 | + url: 'http://localhost:8378/1/validate_purchase', |
| 121 | + json: true, |
| 122 | + body: { |
| 123 | + productIdentifier: "another-product", |
| 124 | + receipt: { |
| 125 | + __type: "Bytes", |
| 126 | + base64: new Buffer("receipt", "utf-8").toString("base64") |
| 127 | + }, |
| 128 | + bypassAppStoreValidation: true |
| 129 | + } |
| 130 | + }, function(err, res, body){ |
| 131 | + if (typeof body != "object") { |
| 132 | + fail("Body is not an object"); |
| 133 | + done(); |
| 134 | + } else { |
| 135 | + expect(body.code).toEqual(Parse.Error.OBJECT_NOT_FOUND); |
| 136 | + expect(body.error).toEqual('Object not found.'); |
| 137 | + done(); |
| 138 | + } |
| 139 | + }); |
| 140 | + }); |
| 141 | + |
| 142 | + it("should fail at appstore validation", (done) => { |
| 143 | + |
| 144 | + request.post({ |
| 145 | + headers: { |
| 146 | + 'X-Parse-Application-Id': 'test', |
| 147 | + 'X-Parse-REST-API-Key': 'rest'}, |
| 148 | + url: 'http://localhost:8378/1/validate_purchase', |
| 149 | + json: true, |
| 150 | + body: { |
| 151 | + productIdentifier: "a-product", |
| 152 | + receipt: { |
| 153 | + __type: "Bytes", |
| 154 | + base64: new Buffer("receipt", "utf-8").toString("base64") |
| 155 | + }, |
| 156 | + } |
| 157 | + }, function(err, res, body){ |
| 158 | + if (typeof body != "object") { |
| 159 | + fail("Body is not an object"); |
| 160 | + } else { |
| 161 | + expect(body.status).toBe(21002); |
| 162 | + expect(body.error).toBe('The data in the receipt-data property was malformed or missing.'); |
| 163 | + } |
| 164 | + done(); |
| 165 | + }); |
| 166 | + }); |
| 167 | + |
| 168 | + it("should not create a _Product", (done) => { |
| 169 | + var product = new Parse.Object("_Product"); |
| 170 | + product.save().then(function(){ |
| 171 | + fail("Should not be able to save"); |
| 172 | + done(); |
| 173 | + }, function(err){ |
| 174 | + expect(err.code).toEqual(Parse.Error.INCORRECT_TYPE); |
| 175 | + done(); |
| 176 | + }) |
| 177 | + }); |
| 178 | + |
| 179 | + it("should be able to update a _Product", (done) => { |
| 180 | + var query = new Parse.Query("_Product"); |
| 181 | + query.first().then(function(product){ |
| 182 | + product.set("title", "a new title"); |
| 183 | + return product.save(); |
| 184 | + }).then(function(productAgain){ |
| 185 | + expect(productAgain.get('downloadName')).toEqual(productAgain.get('download').name()); |
| 186 | + expect(productAgain.get("title")).toEqual("a new title"); |
| 187 | + done(); |
| 188 | + }).fail(function(err){ |
| 189 | + fail(JSON.stringify(err)); |
| 190 | + done(); |
| 191 | + }); |
| 192 | + }); |
| 193 | + |
| 194 | + it("should not be able to remove a require key in a _Product", (done) => { |
| 195 | + var query = new Parse.Query("_Product"); |
| 196 | + query.first().then(function(product){ |
| 197 | + product.unset("title"); |
| 198 | + return product.save(); |
| 199 | + }).then(function(productAgain){ |
| 200 | + fail("Should not succeed"); |
| 201 | + done(); |
| 202 | + }).fail(function(err){ |
| 203 | + expect(err.code).toEqual(Parse.Error.INCORRECT_TYPE); |
| 204 | + expect(err.message).toEqual("title is required."); |
| 205 | + done(); |
| 206 | + }); |
| 207 | + }); |
| 208 | + |
| 209 | +}); |
0 commit comments