
var request = require('request');
var DatabaseAdapter = require('../src/DatabaseAdapter');

var database = DatabaseAdapter.getDatabaseConnection('test');

describe('a GlobalConfig', () => {
  beforeEach(function(done) {
    database.rawCollection('_GlobalConfig')
      .then(coll => coll.updateOne({ '_id': 1}, { $set: { params: { companies: ['US', 'DK'] } } }, { upsert: true }))
      .then(done());
  });

  it('can be retrieved', (done) => {
    request.get({
      url: 'http://localhost:8378/1/config',
      json: true,
      headers: {
        'X-Parse-Application-Id': 'test',
        'X-Parse-Master-Key': 'test',
      },
    }, (error, response, body) => {
      expect(response.statusCode).toEqual(200);
      expect(body.params.companies).toEqual(['US', 'DK']);
      done();
    });
  });

  it('can be updated when a master key exists', (done) => {
    request.post({
      url: 'http://localhost:8378/1/config',
      json: true,
      body: { params: { companies: ['US', 'DK', 'SE'] } },
      headers: {
        'X-Parse-Application-Id': 'test',
        'X-Parse-Master-Key': 'test'
      },
    }, (error, response, body) => {
      expect(response.statusCode).toEqual(200);
      expect(body.params.companies).toEqual(['US', 'DK', 'SE']);
      done();
    });
  });

  it('fail to update if master key is missing', (done) => {
    request.post({
      url: 'http://localhost:8378/1/config',
      json: true,
      body: { params: { companies: [] } },
      headers: {
        'X-Parse-Application-Id': 'test',
        'X-Parse-REST-API-Key': 'rest'
      },
    }, (error, response, body) => {
      expect(response.statusCode).toEqual(401);
      expect(body.error).toEqual('unauthorized');
      done();
    });
  });  

});

@@ -28,16 +28,57 @@
 uest');%0A
+var Parse = require('parse/node').Parse;%0A
 var Data
@@ -1782,12 +1782,1280 @@
  %7D);  %0A%0A
+  it('failed getting config when it is missing', (done) =%3E %7B%0A    database.rawCollection('_GlobalConfig')%0A      .then(coll =%3E coll.deleteOne(%7B '_id': 1%7D, %7B%7D, %7B%7D))%0A      .then(_ =%3E %7B%0A        request.get(%7B%0A          url: 'http://localhost:8378/1/config',%0A          json: true,%0A          headers: %7B%0A            'X-Parse-Application-Id': 'test',%0A            'X-Parse-Master-Key': 'test',%0A          %7D,%0A        %7D, (error, response, body) =%3E %7B%0A          expect(response.statusCode).toEqual(404);%0A          expect(body.code).toEqual(Parse.Error.INVALID_KEY_NAME);%0A          done();%0A        %7D);%0A      %7D);%0A  %7D);%0A%0A  it('failed updating config when it is missing', (done) =%3E %7B%0A    database.rawCollection('_GlobalConfig')%0A      .then(coll =%3E coll.deleteOne(%7B '_id': 1%7D, %7B%7D, %7B%7D))%0A      .then(_ =%3E %7B%0A        request.post(%7B%0A          url: 'http://localhost:8378/1/config',%0A          json: true,%0A          body: %7B params: %7B companies: %5B'US', 'DK', 'SE'%5D %7D %7D,%0A          headers: %7B%0A            'X-Parse-Application-Id': 'test',%0A            'X-Parse-Master-Key': 'test'%0A          %7D,%0A        %7D, (error, response, body) =%3E %7B%0A          expect(response.statusCode).toEqual(404);%0A          expect(body.code).toEqual(Parse.Error.INVALID_KEY_NAME);%0A          done();%0A        %7D);%0A      %7D);%0A  %7D);%0A%0A
 %7D);%0A

Add test for uncovered cases