mutcianm/IDEA-293570
[idea/community.git] / platform / platform-tests / testSrc / com / intellij / ide / plugins / ExternalAuthContributorServiceTest.kt
1 // Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
2 package com.intellij.ide.plugins
3
4 import com.intellij.ide.plugins.auth.PluginRepositoryAuthProvider
5 import com.intellij.ide.plugins.auth.PluginRepositoryAuthService
6 import com.intellij.testFramework.ApplicationRule
7 import com.intellij.testFramework.DisposableRule
8 import com.intellij.testFramework.ExtensionTestUtil
9 import org.junit.ClassRule
10 import org.junit.Rule
11 import org.junit.Test
12 import kotlin.test.assertEquals
13
14 class PluginRepositoryAuthServiceTest {
15
16   companion object {
17     @JvmField
18     @ClassRule
19     val appRule = ApplicationRule()
20   }
21
22   @Rule
23   @JvmField
24   val disposableRule = DisposableRule()
25
26   private val headers = mapOf("Header" to "DATA")
27   private val fooDomain = "foo.bar"
28   private val fooUrl = "https://foo.bar/foo?a=b&c=d"
29   private val barUrl = "https://bar.baz/baz/zaz"
30
31   private val unescapedUrl = "https://buildserver.labs.intellij.net/guestAuth/repository/download/Documentation_Stardust/lastSuccessful/updatePlugins.xml?branch=master-internal &build=IU-221.3427.103"
32
33   private inner class FakeUrlMatchingContributor(private val myDomain: String,
34                                                  private val contributorHeaders: Map<String, String> = headers): PluginRepositoryAuthProvider {
35     override fun canHandle(url: String): Boolean = (url.contains(myDomain))
36     override fun getAuthHeaders(url: String?): Map<String, String>  = contributorHeaders
37   }
38
39   @Test
40   fun basicTest() {
41     setupContributors(FakeUrlMatchingContributor(fooDomain))
42     assertEquals(headers, PluginRepositoryAuthService().getAllCustomHeaders(fooUrl))
43   }
44
45   @Test
46   fun `no headers are returned for an unmatched URL`() {
47     val authService = PluginRepositoryAuthService()
48     setupContributors(FakeUrlMatchingContributor(fooDomain))
49     assertEquals(headers, authService.getAllCustomHeaders(fooUrl))
50     assertEquals(emptyMap(), authService.getAllCustomHeaders(barUrl))
51   }
52
53   @Test
54   fun `only the first contributor is used in case of multiple matching`() {
55     val authService = PluginRepositoryAuthService()
56     setupContributors(FakeUrlMatchingContributor(fooDomain), FakeUrlMatchingContributor(fooDomain, mapOf("foo" to "bar")))
57     assertEquals(headers, authService.getAllCustomHeaders(fooUrl))
58   }
59
60   @Test
61   fun `malformed URL doesn't crash getAllCustomHeaders`() {
62     val authService = PluginRepositoryAuthService()
63     authService.getAllCustomHeaders(unescapedUrl)
64   }
65
66   @Test
67   fun `getAllCustomHeaders doesn't crash when no contributors match`() {
68     val authService = PluginRepositoryAuthService()
69     setupContributors(FakeUrlMatchingContributor(fooDomain))
70     authService.getAllCustomHeaders(barUrl)
71     authService.getAllCustomHeaders(fooUrl)
72     authService.getAllCustomHeaders(barUrl)
73     assertEquals(headers, authService.getAllCustomHeaders(fooUrl))
74   }
75
76   private fun setupContributors(vararg contributor: PluginRepositoryAuthProvider) {
77     ExtensionTestUtil.maskExtensions(PluginRepositoryAuthProvider.EP_NAME, contributor.asList(), disposableRule.disposable)
78   }
79 }