var chakram = require('chakram'), expect = chakram.expect;
var env = require('../enviroment');
const URL = env.URL;

describe('Geolocation', function() {
  it('should create a geolocation as an android user', function() {
    let credential = {
      'email': 'user@user.com',
      'password': 'user',
    };
    return chakram.post(`${URL}end_users/login`, credential).then(function(loginResponse) {
      let testObject = {
        'category_id': 1,
        'geolocation_name': env.randomWord(10),
        'latitude': 10.2123,
        'longitude': 112.2124214,
        'when_sent': new Date(Date.now()),
        'android_id': env.randomId(20),
        'user_id': loginResponse.body.result.userId,
        'description_location': env.randomWord(6, false, true) + ' ' + env.randomWord(6),
      };
      return chakram.post(`${URL}geolocations`, testObject).then(function(myResponse) {
        expect(myResponse).to.have.status(200);
        expect(myResponse.body.result).to.be.a('object');
      });
    });
  });
  it('should create a geolocation as a logged user', function() {
    let testObject = {
      'category_id': 1,
      'geolocation_name': env.randomWord(10),
      'latitude': 116.2123,
      'longitude': 50.2124214,
      'when_sent': new Date(Date.now()),
      'description_location': env.randomWord(6, false, true) + ' ' + env.randomWord(6),
    };
    return chakram.post(`${URL}geolocations`, testObject).then(function(myResponse) {
      expect(myResponse).to.have.status(200);
      expect(myResponse.body.result).to.be.a('object');
    });
  });
  it('should return all geolocations', function() {
    return chakram.get(`${URL}geolocations`).then(function(myResponse) {
      expect(myResponse).to.have.status(200);
      expect(myResponse.body.result).to.be.a('array');
    });
  });
  it('should return one geolocation', function() {
    return chakram.get(`${URL}geolocations`).then(function(myResponse) {
      let geolocation = myResponse.body.result[0];
      return chakram.get(`${URL}geolocations/${myResponse.body.result[0].id}`).then(function(myResponse2) {
        expect(myResponse2).to.have.status(200);
        expect(myResponse2.body.result.geolocation_name).to.contain(geolocation.geolocation_name);
      });
    });
  });
  it('should try to remove one geolocation', function() {
    return chakram.get(`${URL}geolocations`).then(function(myResponse) {
      let id = myResponse.body.result[0].id;
      return chakram.delete(`${URL}geolocations/${id}`).then(function(myResponse2) {
        expect(myResponse2).to.have.status(401);
        expect(myResponse2.body.error.code).to.contain('AUTHORIZATION_REQUIRED');
      });
    });
  });
});