<commit_before>/*
 * Copyright 2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.gradle.integtests.resolve.http

import org.gradle.integtests.fixtures.HttpServer
import org.hamcrest.Matchers
import spock.lang.Unroll
import org.gradle.integtests.resolve.AbstractDependencyResolutionTest

class HttpAuthenticationDependencyResolutionIntegrationTest extends AbstractDependencyResolutionTest {
    static String badCredentials = "credentials{username 'testuser'; password '<PASSWORD>'}"

    @Unroll
    public void "can resolve dependencies from #authScheme authenticated HTTP ivy repository"() {
        server.start()
        given:
        def module = ivyRepo().module('group', 'projectA', '1.2').publish()

        and:
        buildFile << """
repositories {
    ivy {
        url "http://localhost:${server.port}/repo"

        credentials {
            password 'password'
            username 'username'
        }
    }
}
configurations { compile }
dependencies {
    compile 'group:projectA:1.2'
}
task listJars << {
    assert configurations.compile.collect { it.name } == ['projectA-1.2.jar']
}
"""

        when:
        server.authenticationScheme = authScheme

        and:
        server.expectGet('/repo/group/projectA/1.2/ivy-1.2.xml', 'username', 'password', module.ivyFile)
        server.expectGet('/repo/group/projectA/1.2/projectA-1.2.jar', 'username', 'password', module.jarFile)

        then:
        succeeds('listJars')

        where:
        authScheme << [HttpServer.AuthScheme.BASIC, HttpServer.AuthScheme.DIGEST]
    }

    @Unroll
    public void "can resolve dependencies from #authScheme authenticated HTTP maven repository"() {
        server.start()
        given:
        def moduleA = mavenRepo().module('group', 'projectA', '1.2').publish()
        def moduleB = mavenRepo().module('group', 'projectB', '2.1').publishWithRootMavenMetaData()
        and:
        buildFile << """
repositories {
    maven {
        url "http://localhost:${server.port}/repo"

        credentials {
            password 'password'
            username 'username'
        }
    }
}
configurations { compile }
dependencies {
    compile 'group:projectA:1.2'
    compile 'group:projectB:2.+'
}
task listJars << {
    assert configurations.compile.collect { it.name } == ['projectA-1.2.jar', 'projectB-2.1.jar']
}
"""

        when:
        server.authenticationScheme = authScheme

        and:
        server.expectGet('/repo/group/projectA/1.2/projectA-1.2.pom'  , 'username', 'password', moduleA.pomFile)
        server.expectGet('/repo/group/projectA/1.2/projectA-1.2.jar'  , 'username', 'password', moduleA.artifactFile)
        server.expectGet('/repo/group/projectB/maven-metadata.xml'    , 'username', 'password', moduleB.rootMetaDataFile)
        server.expectGet('/repo/group/projectB/2.1/projectB-2.1.pom'  , 'username', 'password', moduleB.pomFile)
        server.expectGet('/repo/group/projectB/2.1/projectB-2.1.jar'  , 'username', 'password', moduleB.artifactFile)

        then:
        succeeds('listJars')

        where:
        authScheme << [HttpServer.AuthScheme.BASIC, HttpServer.AuthScheme.DIGEST]
    }


    @Unroll
    def "reports failure resolving with #credsName credentials from #authScheme authenticated HTTP maven repository"() {
        given:
        server.start()

        and:
        def module = mavenRepo().module('group', 'projectA', '1.2').publish()

        when:
        settingsFile << 'rootProject.name = "publish"'
        buildFile << """
 repositories {
     maven {
         url "http://localhost:${server.port}/repo"
        $creds
     }
 }
 configurations { compile }
 dependencies {
     compile 'group:projectA:1.2'
 }
 task listJars << {
     assert configurations.compile.collect { it.name } == ['projectA-1.2.jar']
 }
 """

        and:
        server.authenticationScheme = authScheme
        server.allowGet('/repo/group/projectA/1.2/projectA-1.2.pom', 'username', 'password', module.pomFile)

        then:
        fails 'listJars'

        and:
        failure.assertHasDescription('Execution failed for task \':listJars\'.')
        failure.assertHasCause('Could not resolve all dependencies for configuration \':compile\'.')
        failure.assertThatCause(Matchers.containsString('Received status code 401 from server: Unauthorized'))

        where:
        authScheme << [HttpServer.AuthScheme.BASIC, HttpServer.AuthScheme.DIGEST, HttpServer.AuthScheme.BASIC, HttpServer.AuthScheme.DIGEST]
        credsName << ['empty', 'empty' , 'bad', 'bad']
        creds << ['', '', badCredentials, badCredentials]
    }
}
<commit_msg>GRADLE-2318: Add coverage for resolving unique snapshots versions from authenticated http maven repo.<commit_after>/*
 * Copyright 2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.gradle.integtests.resolve.http

import org.gradle.integtests.fixtures.HttpServer
import org.hamcrest.Matchers
import spock.lang.Unroll
import org.gradle.integtests.resolve.AbstractDependencyResolutionTest

class HttpAuthenticationDependencyResolutionIntegrationTest extends AbstractDependencyResolutionTest {
    static String badCredentials = "credentials{username 'testuser'; password '<PASSWORD>'}"

    @Unroll
    public void "can resolve dependencies from #authScheme authenticated HTTP ivy repository"() {
        server.start()
        given:
        def module = ivyRepo().module('group', 'projectA', '1.2').publish()

        and:
        buildFile << """
repositories {
    ivy {
        url "http://localhost:${server.port}/repo"

        credentials {
            password 'password'
            username 'username'
        }
    }
}
configurations { compile }
dependencies {
    compile 'group:projectA:1.2'
}
task listJars << {
    assert configurations.compile.collect { it.name } == ['projectA-1.2.jar']
}
"""

        when:
        server.authenticationScheme = authScheme

        and:
        server.expectGet('/repo/group/projectA/1.2/ivy-1.2.xml', 'username', 'password', module.ivyFile)
        server.expectGet('/repo/group/projectA/1.2/projectA-1.2.jar', 'username', 'password', module.jarFile)

        then:
        succeeds('listJars')

        where:
        authScheme << [HttpServer.AuthScheme.BASIC, HttpServer.AuthScheme.DIGEST]
    }

    @Unroll
    public void "can resolve dependencies from #authScheme authenticated HTTP maven repository"() {
        server.start()
        given:
        def moduleA = mavenRepo().module('group', 'projectA', '1.2').publish()
        def moduleB = mavenRepo().module('group', 'projectB', '2.1').publishWithRootMavenMetaData()
        def moduleC = mavenRepo().module('group', 'projectC', '3.1-SNAPSHOT').publish()
        and:
        buildFile << """
repositories {
    maven {
        url "http://localhost:${server.port}/repo"

        credentials {
            password 'password'
            username 'username'
        }
    }
}
configurations { compile }
dependencies {
    compile 'group:projectA:1.2'
    compile 'group:projectB:2.+'
    compile 'group:projectC:3.1-SNAPSHOT'
}
task listJars << {
    assert configurations.compile.collect { it.name } == ['projectA-1.2.jar', 'projectB-2.1.jar', 'projectC-3.1-SNAPSHOT.jar']
}
"""

        when:
        server.authenticationScheme = authScheme

        and:
        println "MODULEC_VERSION" + moduleC.getPublishArtifactVersion()
        server.expectGet('/repo/group/projectA/1.2/projectA-1.2.pom' , 'username', 'password', moduleA.pomFile)
        server.expectGet('/repo/group/projectA/1.2/projectA-1.2.jar' , 'username', 'password', moduleA.artifactFile)
        server.expectGet('/repo/group/projectB/maven-metadata.xml' , 'username', 'password', moduleB.rootMetaDataFile)
        server.expectGet('/repo/group/projectB/2.1/projectB-2.1.pom' , 'username', 'password', moduleB.pomFile)
        server.expectGet('/repo/group/projectB/2.1/projectB-2.1.jar' , 'username', 'password', moduleB.artifactFile)
        server.expectGet('/repo/group/projectC/3.1-SNAPSHOT/maven-metadata.xml' , 'username', 'password', moduleC.metaDataFile) // TODO: double check why we have two get requests here.
        server.expectGet('/repo/group/projectC/3.1-SNAPSHOT/maven-metadata.xml' , 'username', 'password', moduleC.metaDataFile)
        server.expectGet("/repo/group/projectC/3.1-SNAPSHOT/projectC-${moduleC.getPublishArtifactVersion()}.pom"  , 'username', 'password', moduleC.pomFile)
        server.expectGet("/repo/group/projectC/3.1-SNAPSHOT/projectC-${moduleC.getPublishArtifactVersion()}.jar"  , 'username', 'password', moduleC.artifactFile)

        then:
        succeeds('listJars')

        where:
        authScheme << [HttpServer.AuthScheme.BASIC, HttpServer.AuthScheme.DIGEST]
    }


    @Unroll
    def "reports failure resolving with #credsName credentials from #authScheme authenticated HTTP maven repository"() {
        given:
        server.start()

        and:
        def module = mavenRepo().module('group', 'projectA', '1.2').publish()

        when:
        settingsFile << 'rootProject.name = "publish"'
        buildFile << """
 repositories {
     maven {
         url "http://localhost:${server.port}/repo"
        $creds
     }
 }
 configurations { compile }
 dependencies {
     compile 'group:projectA:1.2'
 }
 task listJars << {
     assert configurations.compile.collect { it.name } == ['projectA-1.2.jar']
 }
 """

        and:
        server.authenticationScheme = authScheme
        server.allowGet('/repo/group/projectA/1.2/projectA-1.2.pom', 'username', 'password', module.pomFile)

        then:
        fails 'listJars'

        and:
        failure.assertHasDescription('Execution failed for task \':listJars\'.')
        failure.assertHasCause('Could not resolve all dependencies for configuration \':compile\'.')
        failure.assertThatCause(Matchers.containsString('Received status code 401 from server: Unauthorized'))

        where:
        authScheme << [HttpServer.AuthScheme.BASIC, HttpServer.AuthScheme.DIGEST, HttpServer.AuthScheme.BASIC, HttpServer.AuthScheme.DIGEST]
        credsName << ['empty', 'empty' , 'bad', 'bad']
        creds << ['', '', badCredentials, badCredentials]
    }
}

{'fraction_non_alphanumeric': 0.1302304964539007, 'fraction_numerical': 0.012588652482269504, 'mean_word_length': 3.0404727793696273, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 6}, 'pii_count': 0, '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': '989768', 'n_tokens_mistral': 3375, 'n_tokens_neox': 3284, 'n_words': 715}