<commit_before>"use strict";
var webdav = require('../../lib/index.js'),
    request = require('request'),
    Client = require('webdav-fs');

module.exports = (test, options, index) => test('lock', isValid =>
{
    var server = new webdav.WebDAVServer();
    server.start(options.port + index);
    isValid = isValid.multiple(6, server);
    const _ = (e, cb) => {
        if(e)
            isValid(false, e);
        else
            cb();
    }
    
    server.userManager.addUser('usernameX', 'password');
    server.userManager.addUser('usernameX2', '<PASSWORD>');

    const url = 'http://127.0.0.1:' + (options.port + index);
    const wfsOwner = Client(url, 'usernameX', 'password');
    const wfsNotOwner = Client(url, 'usernameX2', '<PASSWORD>');
    
    server.rootResource.addChild(new webdav.VirtualFile('test.txt'), e => _(e, () => {
        wfsNotOwner.writeFile('/test.txt', 'Content!', (e) => _(e, () => {
            request({
                url: url + '/test.txt',
                method: 'LOCK',
                headers: {
                    Authorization: 'Basic dXNlcm5hbWVYOnBhc3N3b3Jk'
                },
                body: '<?xml version="1.0" encoding="utf-8" ?><D:lockinfo xmlns:D="DAV:"><D:lockscope><D:exclusive/></D:lockscope><D:locktype><D:write/></D:locktype><D:owner><D:href>'+url+'/user</D:href></D:owner></D:lockinfo>'
            }, (e, res, body) => _(e, () => {
                if(res.statusCode !== 200)
                {
                    isValid(false, res.statusMessage);
                    return;
                }
                
                wfsNotOwner.writeFile('/test.txt', 'Content!', (e) => {
                    isValid(!!e, 'Should not be able to write in a locked file');
                })
            }))
        }))
    }))
    
    server.rootResource.addChild(new webdav.VirtualFile('test2.txt'), e => _(e, () => {
        wfsOwner.writeFile('/test2.txt', 'Content!', (e) => _(e, () => {
            request({
                url: url + '/test2.txt',
                method: 'LOCK',
                headers: {
                    Authorization: 'Basic <KEY>'
                },
                body: '<?xml version="1.0" encoding="utf-8" ?><D:lockinfo xmlns:D="DAV:"><D:lockscope><D:exclusive/></D:lockscope><D:locktype><D:write/></D:locktype><D:owner><D:href>'+url+'/user</D:href></D:owner></D:lockinfo>'
            }, (e, res, body) => _(e, () => {
                if(res.statusCode !== 200)
                {
                    isValid(false, res.statusMessage);
                    return;
                }
                
                wfsOwner.writeFile('/test2.txt', 'Content!', (e) => {
                    isValid(!e, 'Could not write in its own locked file');
                })
            }))
        }))
    }))
    
    server.rootResource.addChild(new webdav.VirtualFile('test3.txt'), e => _(e, () => {
        request({
            url: url + '/test3.txt',
            method: 'LOCK',
            headers: {
                Authorization: 'Basic <KEY>'
            },
            body: '<?xml version="1.0" encoding="utf-8" ?><D:lockinfo xmlns:D="DAV:"><D:lockscope><D:exclusive/></D:lockscope><D:locktype><D:write/></D:locktype><D:owner><D:href>'+url+'/user</D:href></D:owner></D:lockinfo>'
        }, (e, res, body) => _(e, () => {
            if(res.statusCode !== 200)
            {
                isValid(false, 'LOCK on a locked resource must lead to a 200 OK');
                return;
            }

            request({
                url: url + '/test3.txt',
                method: 'LOCK',
                headers: {
                    Authorization: 'Basic dXNlcm5hbWVYOnBhc3N3b3Jk'
                },
                body: '<?xml version="1.0" encoding="utf-8" ?><D:lockinfo xmlns:D="DAV:"><D:lockscope><D:exclusive/></D:lockscope><D:locktype><D:write/></D:locktype><D:owner><D:href>'+url+'/user</D:href></D:owner></D:lockinfo>'
            }, (e, res, body) => _(e, () => {
                isValid(res.statusCode === 423, 'LOCK on a locked resource must lead to a 423 Locked');
            }))
        }))
    }))
    /*
    server.rootResource.addChild(new webdav.VirtualFile('test4.txt'), e => _(e, () => {
        request({
            url: url + '/test4.txt',
            method: 'LOCK',
            headers: {
                Authorization: 'Basic <KEY>'
            },
            body: '<?xml version="1.0" encoding="utf-8" ?><D:lockinfo xmlns:D="DAV:"><D:lockscope><D:exclusive/></D:lockscope><D:locktype><D:write/></D:locktype><D:owner><D:href>'+url+'/user</D:href></D:owner></D:lockinfo>'
        }, (e, res, body) => _(e, () => {
            const lock = body.substr(body.indexOf('<D:locktoken><D:href>') + '<D:locktoken><D:href>'.length, 'urn:uuid:24fa520c-520c-14fa-00d6-0000d546f655'.length);
            
            request({
                url: url + '/test4.txt',
                method: 'LOCK',
                headers: {
                    Authorization: 'Basic dXNlcm5hbWVYOnBhc3N3b3Jk'
                },
                body: '<?xml version="1.0" encoding="utf-8" ?><D:lockinfo xmlns:D="DAV:"><D:lockscope><D:exclusive/></D:lockscope><D:locktype><D:write/></D:locktype><D:owner><D:href>'+url+'/user</D:href></D:owner></D:lockinfo>'
            }, (e, res, body) => _(e, () => {
                if(res.statusCode !== 412)
                {
                    isValid(res.statusCode === 412, 'LOCK on a locked resource without a proper If header must lead to a 412 Precondition Failed');
                    return;
                }

                request({
                    url: url + '/test4.txt',
                    method: 'LOCK',
                    headers: {
                        If: '(<' + lock + '>)',
                        Authorization: 'Basic <KEY>'
                    },
                    body: '<?xml version="1.0" encoding="utf-8" ?><D:lockinfo xmlns:D="DAV:"><D:lockscope><D:exclusive/></D:lockscope><D:locktype><D:write/></D:locktype><D:owner><D:href>'+url+'/user</D:href></D:owner></D:lockinfo>'
                }, (e, res, body) => _(e, () => {
                    isValid(res.statusCode === 200, 'LOCK on a locked resource with a If header in order to refresh the lock must lead to a 200 OK');
                }))
            }))
        }))
    }))*/
    
    request({
        url: url + '/testDoesNotExist.txt',
        method: 'LOCK',
        headers: {
            Authorization: 'Basic <KEY>'
        },
        body: '<?xml version="1.0" encoding="utf-8" ?><D:lockinfo xmlns:D="DAV:"><D:lockscope><D:exclusive/></D:lockscope><D:locktype><D:write/></D:locktype><D:owner><D:href>'+url+'/user</D:href></D:owner></D:lockinfo>'
    }, (e, res, body) => _(e, () => {
        isValid(res.statusCode === 201, 'LOCK on an unexisting resource must lead to a 201 Created');
    }))<commit_msg>Added the 'If' header test for the LOCK test + Added the tests for the Lock refresh<commit_after>"use strict";
var webdav = require('../../lib/index.js'),
    request = require('request'),
    Client = require('webdav-fs');

module.exports = (test, options, index) => test('lock', isValid =>
{
    var server = new webdav.WebDAVServer();
    server.start(options.port + index);
    isValid = isValid.multiple(6, server);
    const _ = (e, cb) => {
        if(e)
            isValid(false, e);
        else
            cb();
    }
    
    server.userManager.addUser('usernameX', 'password');
    server.userManager.addUser('usernameX2', '<PASSWORD>');

    const url = 'http://127.0.0.1:' + (options.port + index);
    const wfsOwner = Client(url, 'usernameX', 'password');
    const wfsNotOwner = Client(url, 'usernameX2', '<PASSWORD>');
    
    server.rootResource.addChild(new webdav.VirtualFile('test.txt'), e => _(e, () => {
        wfsNotOwner.writeFile('/test.txt', 'Content!', (e) => _(e, () => {
            request({
                url: url + '/test.txt',
                method: 'LOCK',
                headers: {
                    Authorization: 'Basic dXNlcm5hbWVYOnBhc3N3b3Jk'
                },
                body: '<?xml version="1.0" encoding="utf-8" ?><D:lockinfo xmlns:D="DAV:"><D:lockscope><D:exclusive/></D:lockscope><D:locktype><D:write/></D:locktype><D:owner><D:href>'+url+'/user</D:href></D:owner></D:lockinfo>'
            }, (e, res, body) => _(e, () => {
                if(res.statusCode !== 200)
                {
                    isValid(false, res.statusMessage);
                    return;
                }
                
                wfsNotOwner.writeFile('/test.txt', 'Content!', (e) => {
                    isValid(!!e, 'Should not be able to write in a locked file');
                })
            }))
        }))
    }))
    
    server.rootResource.addChild(new webdav.VirtualFile('test2.txt'), e => _(e, () => {
        wfsOwner.writeFile('/test2.txt', 'Content!', (e) => _(e, () => {
            request({
                url: url + '/test2.txt',
                method: 'LOCK',
                headers: {
                    Authorization: 'Basic <KEY>'
                },
                body: '<?xml version="1.0" encoding="utf-8" ?><D:lockinfo xmlns:D="DAV:"><D:lockscope><D:exclusive/></D:lockscope><D:locktype><D:write/></D:locktype><D:owner><D:href>'+url+'/user</D:href></D:owner></D:lockinfo>'
            }, (e, res, body) => _(e, () => {
                if(res.statusCode !== 200)
                {
                    isValid(false, res.statusMessage);
                    return;
                }
                
                wfsOwner.writeFile('/test2.txt', 'Content!', (e) => {
                    isValid(!e, 'Could not write in its own locked file');
                })
            }))
        }))
    }))
    
    server.rootResource.addChild(new webdav.VirtualFile('test3.txt'), e => _(e, () => {
        request({
            url: url + '/test3.txt',
            method: 'LOCK',
            headers: {
                Authorization: 'Basic <KEY>'
            },
            body: '<?xml version="1.0" encoding="utf-8" ?><D:lockinfo xmlns:D="DAV:"><D:lockscope><D:exclusive/></D:lockscope><D:locktype><D:write/></D:locktype><D:owner><D:href>'+url+'/user</D:href></D:owner></D:lockinfo>'
        }, (e, res, body) => _(e, () => {
            if(res.statusCode !== 200)
            {
                isValid(false, 'LOCK on a locked resource must lead to a 200 OK');
                return;
            }

            request({
                url: url + '/test3.txt',
                method: 'LOCK',
                headers: {
                    Authorization: 'Basic <KEY>'
                },
                body: '<?xml version="1.0" encoding="utf-8" ?><D:lockinfo xmlns:D="DAV:"><D:lockscope><D:exclusive/></D:lockscope><D:locktype><D:write/></D:locktype><D:owner><D:href>'+url+'/user</D:href></D:owner></D:lockinfo>'
            }, (e, res, body) => _(e, () => {
                isValid(res.statusCode === 423, 'LOCK on a locked resource must lead to a 423 Locked');
            }))
        }))
    }))
    
    server.rootResource.addChild(new webdav.VirtualFile('test4.txt'), e => _(e, () => {
        request({
            url: url + '/test4.txt',
            method: 'LOCK',
            headers: {
                Authorization: 'Basic <KEY>'
            },
            body: '<?xml version="1.0" encoding="utf-8" ?><D:lockinfo xmlns:D="DAV:"><D:lockscope><D:exclusive/></D:lockscope><D:locktype><D:write/></D:locktype><D:owner><D:href>'+url+'/user</D:href></D:owner></D:lockinfo>'
        }, (e, res, body) => _(e, () => {
            const lock = body.substr(body.indexOf('<D:locktoken><D:href>') + '<D:locktoken><D:href>'.length, 'urn:uuid:24fa520c-520c-14fa-00d6-0000d546f655'.length);
            
            request({
                url: url + '/test4.txt',
                method: 'LOCK',
                headers: {
                    Authorization: 'Basic <KEY>'
                }
            }, (e, res, body) => _(e, () => {
                if(res.statusCode !== 428)
                {
                    isValid(res.statusCode === 428, 'LOCK on a locked resource without a proper If header must lead to a 428 Precondition Required');
                    return;
                }

                request({
                    url: url + '/test4.txt',
                    method: 'LOCK',
                    headers: {
                        If: '(<' + lock + '>)',
                        Authorization: 'Basic <KEY>'
                    }
                }, (e, res, body) => _(e, () => {
                    if(res.statusCode !== 200)
                    {
                        isValid(res.statusCode === 200, 'LOCK on a locked resource with a If header in order to refresh the lock must lead to a 200 OK');
                        return;
                    }

                    request({
                        url: url + '/test4.txt',
                        method: 'LOCK',
                        headers: {
                            If: '(<' + lock + 'bad>)',
                            Authorization: 'Basic <KEY>'
                        }
                    }, (e, res, body) => _(e, () => {
                        isValid(res.statusCode === 412, 'LOCK on a locked resource without a proper If header must lead to a 412 Precondition Failed');
                    }))
                }))
            }))
        }))
    }))
    
    request({
        url: url + '/testDoesNotExist.txt',
        method: 'LOCK',
        headers: {
            Authorization: 'Basic <KEY>'
        },
        body: '<?xml version="1.0" encoding="utf-8" ?><D:lockinfo xmlns:D="DAV:"><D:lockscope><D:exclusive/></D:lockscope><D:locktype><D:write/></D:locktype><D:owner><D:href>'+url+'/user</D:href></D:owner></D:lockinfo>'
    }, (e, res, body) => _(e, () => {
        isValid(res.statusCode === 201, 'LOCK on an unexisting resource must lead to a 201 Created');
    }))
{'fraction_non_alphanumeric': 0.18661213947481706, 'fraction_numerical': 0.01664514277514708, 'mean_word_length': 1.7060764900019414, 'pattern_counts': {'":': 0, '<': 213, '<?xml version=': 14, '>': 281, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 28}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5587540', 'n_tokens_mistral': 4538, 'n_tokens_neox': 4237, 'n_words': 770}