"Fossies" - the Fresh Open Source Software Archive

Member "gradle-8.1.1/subprojects/dependency-management/src/test/groovy/org/gradle/api/internal/artifacts/repositories/AbstractAuthenticationSupportedRepositoryTest.groovy" (20 Apr 2023, 9291 Bytes) of package /linux/misc/gradle-8.1.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Java source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /*
    2  * Copyright 2014 the original author or authors.
    3  *
    4  * Licensed under the Apache License, Version 2.0 (the "License");
    5  * you may not use this file except in compliance with the License.
    6  * You may obtain a copy of the License at
    7  *
    8  *      http://www.apache.org/licenses/LICENSE-2.0
    9  *
   10  * Unless required by applicable law or agreed to in writing, software
   11  * distributed under the License is distributed on an "AS IS" BASIS,
   12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   13  * See the License for the specific language governing permissions and
   14  * limitations under the License.
   15  */
   16 
   17 package org.gradle.api.internal.artifacts.repositories
   18 
   19 import org.gradle.api.Action
   20 import org.gradle.api.InvalidUserDataException
   21 import org.gradle.api.artifacts.repositories.AuthenticationContainer
   22 import org.gradle.api.artifacts.repositories.PasswordCredentials
   23 import org.gradle.api.credentials.AwsCredentials
   24 import org.gradle.api.credentials.Credentials
   25 import org.gradle.api.credentials.HttpHeaderCredentials
   26 import org.gradle.api.internal.CollectionCallbackActionDecorator
   27 import org.gradle.api.internal.artifacts.ivyservice.ivyresolve.ConfiguredModuleComponentRepository
   28 import org.gradle.api.internal.artifacts.ivyservice.ivyresolve.strategy.VersionParser
   29 import org.gradle.api.internal.artifacts.repositories.descriptor.RepositoryDescriptor
   30 import org.gradle.authentication.Authentication
   31 import org.gradle.internal.authentication.DefaultAuthenticationContainer
   32 import org.gradle.internal.credentials.DefaultAwsCredentials
   33 import org.gradle.internal.credentials.DefaultPasswordCredentials
   34 import org.gradle.internal.reflect.Instantiator
   35 import org.gradle.util.internal.ClosureBackedAction
   36 import org.gradle.util.TestUtil
   37 import spock.lang.Specification
   38 
   39 class AbstractAuthenticationSupportedRepositoryTest extends Specification {
   40 
   41     AuthSupportedRepository repo() {
   42         new AuthSupportedRepository(TestUtil.instantiatorFactory().decorateLenient(), new DefaultAuthenticationContainer(TestUtil.instantiatorFactory().decorateLenient(), CollectionCallbackActionDecorator.NOOP))
   43     }
   44 
   45     def "should configure default password credentials using an action only"() {
   46         setup:
   47         DefaultPasswordCredentials passwordCredentials = new DefaultPasswordCredentials()
   48         enhanceCredentials(passwordCredentials, 'username', 'password')
   49 
   50         Instantiator instantiator = Mock()
   51         AuthenticationContainer authenticationContainer = Stub()
   52         instantiator.newInstance(DefaultPasswordCredentials) >> passwordCredentials
   53 
   54         AuthSupportedRepository repo = new AuthSupportedRepository(instantiator, authenticationContainer)
   55 
   56         def configAction = new Action<PasswordCredentials>() {
   57             @Override
   58             void execute(PasswordCredentials credentials) {
   59                 credentials.username = "myUsername"
   60                 credentials.password = "myPassword"
   61             }
   62         }
   63 
   64         when:
   65         repo.credentials(configAction)
   66 
   67         then:
   68         repo.getCredentials(PasswordCredentials.class)
   69         repo.getCredentials(PasswordCredentials.class).username == 'myUsername'
   70         repo.getCredentials(PasswordCredentials.class).password == 'myPassword'
   71     }
   72 
   73     def "getCredentials(Class) instantiates credentials if not yet configured"() {
   74         given:
   75         DefaultAwsCredentials enhancedCredentials = new DefaultAwsCredentials()
   76         enhanceCredentials(enhancedCredentials, 'accessKey', 'secretKey')
   77 
   78         Instantiator instantiator = Mock()
   79         AuthenticationContainer authenticationContainer = Mock()
   80         instantiator.newInstance(DefaultAwsCredentials) >> enhancedCredentials
   81 
   82         AuthSupportedRepository repo = new AuthSupportedRepository(instantiator, authenticationContainer)
   83 
   84         def action = new ClosureBackedAction<DefaultAwsCredentials>({
   85             accessKey = 'key'
   86             secretKey = 'secret'
   87         })
   88 
   89         expect:
   90         repo.getCredentials(AwsCredentials.class) instanceof AwsCredentials
   91 
   92     }
   93 
   94 
   95     def "getCredentials(Class) instantiates the correct credential types "() {
   96         Instantiator instantiator = Mock()
   97         AuthenticationContainer authenticationContainer = Mock()
   98         AuthSupportedRepository repo = new AuthSupportedRepository(instantiator, authenticationContainer)
   99 
  100         when:
  101         repo.getCredentials(credentialType) == credentials
  102 
  103         then:
  104         1 * instantiator.newInstance(_) >> credentials
  105 
  106         where:
  107         credentialType      | credentials
  108         AwsCredentials      | Mock(AwsCredentials)
  109         PasswordCredentials | Mock(PasswordCredentials)
  110     }
  111 
  112     def "getCredentials(Class) throws IllegalArgumentException when setting credentials with different type than already set"() {
  113         Instantiator instantiator = Mock()
  114         AuthenticationContainer authenticationContainer = Mock()
  115         AuthSupportedRepository repo = new AuthSupportedRepository(instantiator, authenticationContainer)
  116         1 * instantiator.newInstance(_) >> credentials
  117 
  118         when:
  119         repo.getCredentials(AwsCredentials)
  120         and:
  121         repo.getCredentials(PasswordCredentials)
  122 
  123         then:
  124         def ex = thrown(IllegalArgumentException)
  125         ex.message == "Given credentials type '$PasswordCredentials.name' does not match actual type '$AwsCredentials.name'"
  126 
  127         where:
  128         credentials << Mock(AwsCredentials)
  129     }
  130 
  131     def "getCredentials(Class) throws IllegalArgumentException when setting credentials with unknown type"() {
  132         AuthSupportedRepository repo = new AuthSupportedRepository(Mock(Instantiator), Mock(AuthenticationContainer))
  133         when:
  134         repo.getCredentials(UnsupportedCredentials)
  135         then:
  136         def ex = thrown(IllegalArgumentException)
  137         ex.message == "Unknown credentials type: '$UnsupportedCredentials.name' (supported types: $PasswordCredentials.name, $AwsCredentials.name and $HttpHeaderCredentials.name)."
  138     }
  139 
  140     def "credentials(Class, Action) creates credentials on demand if required"() {
  141         Instantiator instantiator = Mock()
  142         AuthenticationContainer authenticationContainer = Stub()
  143 
  144         Action action = Mock()
  145         AuthSupportedRepository repo = new AuthSupportedRepository(instantiator, authenticationContainer)
  146 
  147         when:
  148         repo.credentials(credentialType, action)
  149 
  150         then:
  151         1 * instantiator.newInstance(_) >> credentials
  152         1 * action.execute(credentials)
  153         repo.getCredentials(credentialType) == credentials
  154 
  155         where:
  156         credentialType      | credentials
  157         AwsCredentials      | Mock(AwsCredentials)
  158         PasswordCredentials | Mock(PasswordCredentials)
  159     }
  160 
  161     def "can reference alternative credentials"() {
  162         given:
  163         Instantiator instantiator = Mock()
  164         AuthenticationContainer authenticationContainer = Stub()
  165 
  166         Action action = Mock()
  167         def credentials = Mock(AwsCredentials)
  168         1 * instantiator.newInstance(_) >> credentials
  169         1 * action.execute(credentials)
  170 
  171         AuthSupportedRepository repo = new AuthSupportedRepository(instantiator, authenticationContainer)
  172         when:
  173         repo.credentials(AwsCredentials, action)
  174 
  175         then:
  176         repo.getCredentials(AwsCredentials) instanceof AwsCredentials
  177     }
  178 
  179     def "get credentials throws ISE if not using password credentials"() {
  180         when:
  181         repo().with {
  182             credentials(AwsCredentials, {})
  183             credentials
  184         }
  185 
  186         then:
  187         thrown IllegalStateException
  188     }
  189 
  190     def "credentials(Action) throws ISE if not using password credentials"() {
  191         when:
  192         repo().with {
  193             credentials(AwsCredentials, {})
  194             credentials {}
  195         }
  196 
  197         then:
  198         thrown IllegalStateException
  199     }
  200 
  201     def "authentication container throws IUD with authentication of unknown type"() {
  202         def action = new Action<AuthenticationContainer>() {
  203             @Override
  204             void execute(AuthenticationContainer authentications) {
  205                 authentications.create('basic', UnsupportedAuthentication)
  206             }
  207         }
  208 
  209         when:
  210         repo().with {
  211             authentication(action)
  212         }
  213 
  214         then:
  215         thrown InvalidUserDataException
  216     }
  217 
  218     private void enhanceCredentials(Credentials credentials, String... props) {
  219         props.each { prop ->
  220             credentials.metaClass."$prop" = { String val ->
  221                 delegate."set${prop.capitalize()}"(val)
  222             }
  223         }
  224     }
  225 
  226     class AuthSupportedRepository extends AbstractAuthenticationSupportedRepository {
  227         AuthSupportedRepository(Instantiator instantiator, AuthenticationContainer authenticationContainer) {
  228             super(instantiator, authenticationContainer, TestUtil.objectFactory(), null, new VersionParser())
  229         }
  230 
  231         @Override
  232         protected RepositoryDescriptor createDescriptor() {
  233             throw new UnsupportedOperationException()
  234         }
  235 
  236         @Override
  237         ConfiguredModuleComponentRepository createResolver() {
  238             throw new UnsupportedOperationException()
  239         }
  240     }
  241 
  242     interface UnsupportedCredentials extends Credentials {}
  243 
  244     interface UnsupportedAuthentication extends Authentication {}
  245 }