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('adding + updating name', () => {
    let added_org_id
    let added_org
    it('creating a new org with just a name should return 200', async () => {
        const res = await axios.post(base + '/api/organizations', {
            "name": "test123"
        }, axios_config);
        added_org = res.data
        added_org_id = added_org.id;
        expect(res.status).toEqual(200);
        expect(res.headers['content-type']).toContain("application/json")
    });
    it('update org', async () => {
        const res = await axios.put(base + '/api/organizations/' + added_org_id, {
            "id": added_org_id,
            "name": "testlelele",
            "contact": null,
            "address": null,
        }, axios_config);
        expect(res.status).toEqual(200);
        expect(res.headers['content-type']).toContain("application/json")
        let added_org2 = res.data
        added_org_id = added_org2.id;
        delete added_org2.id
        expect(added_org2).toEqual({
            "name": "testlelele",
            "address": {
                "address1": null,
                "address2": null,
                "city": null,
                "country": null,
                "postalcode": null,
            },
            "registrationEnabled": false,
            "teams": [],
            "responseType": "RUNNERORGANIZATION"
        })
    });
});
// ---------------
describe('adding + try updating id (should return 406)', () => {
    let added_org_id
    let added_org
    it('creating a new org with just a name should return 200', async () => {
        const res = await axios.post(base + '/api/organizations', {
            "name": "test123"
        }, axios_config);
        added_org = res.data
        added_org_id = added_org.id;
        expect(res.status).toEqual(200);
        expect(res.headers['content-type']).toContain("application/json")
    });
    it('update org', async () => {
        const res = await axios.put(base + '/api/organizations/' + added_org_id, {
            "id": added_org_id + 1,
            "name": "testlelele",
            "contact": null,
            "address": null,
        }, axios_config);
        expect(res.status).toEqual(406);
        expect(res.headers['content-type']).toContain("application/json")
    });
});
// ---------------
describe('adding + updateing address valid)', () => {
    let added_org_id
    let added_org
    it('creating a new org with just a name should return 200', async () => {
        const res = await axios.post(base + '/api/organizations', {
            "name": "test123"
        }, axios_config);
        added_org = res.data
        added_org_id = added_org.id;
        expect(res.status).toEqual(200);
        expect(res.headers['content-type']).toContain("application/json")
    });
    it('adding address to org should return 200', async () => {
        const res = await axios.put(base + '/api/organizations/' + added_org_id, {
            "id": added_org_id,
            "name": "testlelele",
            "contact": null,
            "address": {
                "address1": "Test1",
                "address2": null,
                "city": "Herzogenaurach",
                "country": "Burkina Faso",
                "postalcode": "90174"
            }
        }, axios_config);
        expect(res.status).toEqual(200);
        expect(res.headers['content-type']).toContain("application/json");
        expect(res.data).toEqual({
            "id": added_org_id,
            "name": "testlelele",
            "address": {
                "address1": "Test1",
                "address2": null,
                "city": "Herzogenaurach",
                "country": "Burkina Faso",
                "postalcode": "90174"
            },
            "registrationEnabled": false,
            "teams": [],
            "responseType": "RUNNERORGANIZATION"
        });
    });
    it('updateing address\'s first line should return 200', async () => {
        const res = await axios.put(base + '/api/organizations/' + added_org_id, {
            "id": added_org_id,
            "name": "testlelele",
            "contact": null,
            "address": {
                "address1": "Test2",
                "address2": null,
                "city": "TestCity",
                "country": "Burkina Faso",
                "postalcode": "90174"
            }
        }, axios_config);
        expect(res.status).toEqual(200);
        expect(res.headers['content-type']).toContain("application/json");
        expect(res.data).toEqual({
            "id": added_org_id,
            "name": "testlelele",
            "address": {
                "address1": "Test2",
                "address2": null,
                "city": "TestCity",
                "country": "Burkina Faso",
                "postalcode": "90174"
            },
            "registrationEnabled": false,
            "teams": [],
            "responseType": "RUNNERORGANIZATION"
        });
    });
    it('updateing address\'s second line should return 200', async () => {
        const res = await axios.put(base + '/api/organizations/' + added_org_id, {
            "id": added_org_id,
            "name": "testlelele",
            "contact": null,
            "address": {
                "address1": "Test2",
                "address2": "Test3",
                "city": "TestCity",
                "country": "Burkina Faso",
                "postalcode": "90174"
            }
        }, axios_config);
        expect(res.status).toEqual(200);
        expect(res.headers['content-type']).toContain("application/json");
        expect(res.data).toEqual({
            "id": added_org_id,
            "name": "testlelele",
            "address": {
                "address1": "Test2",
                "address2": "Test3",
                "city": "TestCity",
                "country": "Burkina Faso",
                "postalcode": "90174"
            },
            "registrationEnabled": false,
            "teams": [],
            "responseType": "RUNNERORGANIZATION"
        });
    });
    it('updateing address\'s city should return 200', async () => {
        const res = await axios.put(base + '/api/organizations/' + added_org_id, {
            "id": added_org_id,
            "name": "testlelele",
            "contact": null,
            "address": {
                "address1": "Test2",
                "address2": "Test3",
                "city": "Kaya",
                "country": "Burkina Faso",
                "postalcode": "90174"
            }
        }, axios_config);
        expect(res.status).toEqual(200);
        expect(res.headers['content-type']).toContain("application/json");
        expect(res.data).toEqual({
            "id": added_org_id,
            "name": "testlelele",
            "address": {
                "address1": "Test2",
                "address2": "Test3",
                "city": "Kaya",
                "country": "Burkina Faso",
                "postalcode": "90174"
            },
            "registrationEnabled": false,
            "teams": [],
            "responseType": "RUNNERORGANIZATION"
        });
    });
    it('updateing address\'s country should return 200', async () => {
        const res = await axios.put(base + '/api/organizations/' + added_org_id, {
            "id": added_org_id,
            "name": "testlelele",
            "contact": null,
            "address": {
                "address1": "Test2",
                "address2": "Test3",
                "city": "Kaya",
                "country": "Germany",
                "postalcode": "90174"
            }
        }, axios_config);
        expect(res.status).toEqual(200);
        expect(res.headers['content-type']).toContain("application/json");
        expect(res.data).toEqual({
            "id": added_org_id,
            "name": "testlelele",
            "address": {
                "address1": "Test2",
                "address2": "Test3",
                "city": "Kaya",
                "country": "Germany",
                "postalcode": "90174"
            },
            "registrationEnabled": false,
            "teams": [],
            "responseType": "RUNNERORGANIZATION"
        });
    });
    it('updateing address\'s postal code should return 200', async () => {
        const res = await axios.put(base + '/api/organizations/' + added_org_id, {
            "id": added_org_id,
            "name": "testlelele",
            "contact": null,
            "address": {
                "address1": "Test2",
                "address2": "Test3",
                "city": "Kaya",
                "country": "Germany",
                "postalcode": "91065"
            }
        }, axios_config);
        expect(res.status).toEqual(200);
        expect(res.headers['content-type']).toContain("application/json");
        expect(res.data).toEqual({
            "id": added_org_id,
            "name": "testlelele",
            "address": {
                "address1": "Test2",
                "address2": "Test3",
                "city": "Kaya",
                "country": "Germany",
                "postalcode": "91065"
            },
            "registrationEnabled": false,
            "teams": [],
            "responseType": "RUNNERORGANIZATION"
        });
    });
    it('removing org\'s address should return 200', async () => {
        const res = await axios.put(base + '/api/organizations/' + added_org_id, {
            "id": added_org_id,
            "name": "testlelele",
            "contact": null,
        }, axios_config);
        expect(res.status).toEqual(200);
        expect(res.headers['content-type']).toContain("application/json");
        expect(res.data).toEqual({
            "id": added_org_id,
            "name": "testlelele",
            "address": {
                "address1": null,
                "address2": null,
                "city": null,
                "country": null,
                "postalcode": null
            },
            "registrationEnabled": false,
            "teams": [],
            "responseType": "RUNNERORGANIZATION"
        });
    });
});
// ---------------
describe('adding + updateing address invalid)', () => {
    let added_org_id
    let added_org
    it('creating a new org with just a name should return 200', async () => {
        const res = await axios.post(base + '/api/organizations', {
            "name": "test123"
        }, axios_config);
        added_org = res.data
        added_org_id = added_org.id;
        expect(res.status).toEqual(200);
        expect(res.headers['content-type']).toContain("application/json")
    });
    it('adding address to org w/o address1 should return 400', async () => {
        const res = await axios.put(base + '/api/organizations/' + added_org_id, {
            "id": added_org_id,
            "name": "testlelele",
            "contact": null,
            "address": {
                "address1": null,
                "address2": null,
                "city": "TestCity",
                "country": "Burkina Faso",
                "postalcode": "90174"
            }
        }, axios_config);
        expect(res.status).toEqual(400);
        expect(res.headers['content-type']).toContain("application/json");
    });
    it('adding address to org w/o city should return 400', async () => {
        const res = await axios.put(base + '/api/organizations/' + added_org_id, {
            "id": added_org_id,
            "name": "testlelele",
            "contact": null,
            "address": {
                "address1": "Test1",
                "address2": null,
                "city": null,
                "country": "Burkina Faso",
                "postalcode": "90174"
            }
        }, axios_config);
        expect(res.status).toEqual(400);
        expect(res.headers['content-type']).toContain("application/json");
    });
    it('adding address to org w/o country should return 400', async () => {
        const res = await axios.put(base + '/api/organizations/' + added_org_id, {
            "id": added_org_id,
            "name": "testlelele",
            "contact": null,
            "address": {
                "address1": "Test1",
                "address2": null,
                "city": "TestCity",
                "country": null,
                "postalcode": "90174"
            }
        }, axios_config);
        expect(res.status).toEqual(400);
        expect(res.headers['content-type']).toContain("application/json");
    });
    it('adding address to org w/o postal code should return 400', async () => {
        const res = await axios.put(base + '/api/organizations/' + added_org_id, {
            "id": added_org_id,
            "name": "testlelele",
            "contact": null,
            "address": {
                "address1": "Test1",
                "address2": null,
                "city": "TestCity",
                "country": "Burkina Faso",
                "postalcode": null
            }
        }, axios_config);
        expect(res.status).toEqual(400);
        expect(res.headers['content-type']).toContain("application/json");
    });
    it('adding address to org w/ invalid postal code should return 400', async () => {
        const res = await axios.put(base + '/api/organizations/' + added_org_id, {
            "id": added_org_id,
            "name": "testlelele",
            "contact": null,
            "address": {
                "address1": "Test1",
                "address2": null,
                "city": "TestCity",
                "country": "Burkina Faso",
                "postalcode": "-1"
            }
        }, axios_config);
        expect(res.status).toEqual(400);
        expect(res.headers['content-type']).toContain("application/json");
    });
});