import axios from 'axios';
import { config } from '../../config';
const base = "http://localhost:" + config.internal_port

let access_token;
let axios_config;

beforeAll(async () => {
    jest.setTimeout(20000);
    const res = await axios.post(base + '/api/auth/login', { username: "demo", password: "demo" });
    access_token = res.data["access_token"];
    axios_config = {
        headers: { "authorization": "Bearer " + access_token },
        validateStatus: undefined
    };
});

describe('Update donor name after adding', () => {
    let added_donor;
    it('creating a new runner with only needed params should return 200', async () => {
        const res2 = await axios.post(base + '/api/donors', {
            "firstname": "first",
            "lastname": "last"
        }, axios_config);
        added_donor = res2.data;
        expect(res2.status).toEqual(200);
        expect(res2.headers['content-type']).toContain("application/json")
    });
    it('valid update should return 200', async () => {
        let donor_copy = added_donor
        donor_copy.firstname = "second"
        const res3 = await axios.put(base + '/api/donors/' + added_donor.id, donor_copy, axios_config);
        expect(res3.status).toEqual(200);
        expect(res3.headers['content-type']).toContain("application/json")
        let updated_donor = res3.data
        expect(updated_donor).toEqual(donor_copy);
    });
});
// ---------------
describe('Update donor id after adding(should fail)', () => {
    let added_donor;
    it('creating a new donor with only needed params should return 200', async () => {
        const res2 = await axios.post(base + '/api/donors', {
            "firstname": "first",
            "lastname": "last"
        }, axios_config);
        added_donor = res2.data;
        expect(res2.status).toEqual(200);
        expect(res2.headers['content-type']).toContain("application/json")
    });
    it('invalid update should return 406', async () => {
        added_donor.id++;
        const res3 = await axios.put(base + '/api/donors/' + (added_donor.id - 1), added_donor, axios_config);
        expect(res3.status).toEqual(406);
        expect(res3.headers['content-type']).toContain("application/json")
    });
});
// ---------------
describe('Update donor without address but receiptNeeded=true should fail', () => {
    let added_donor;
    it('creating a new donor with only needed params should return 200', async () => {
        const res2 = await axios.post(base + '/api/donors', {
            "firstname": "first",
            "lastname": "testtest",
        }, axios_config);
        added_donor = res2.data;
        expect(res2.status).toEqual(200);
        expect(res2.headers['content-type']).toContain("application/json")
    });
    it('invalid update should return 406', async () => {
        added_donor.receiptNeeded = true;
        const res3 = await axios.put(base + '/api/donors/' + added_donor.id, added_donor, axios_config);
        expect(res3.status).toEqual(406);
        expect(res3.headers['content-type']).toContain("application/json")
    });
});