]> source.dussan.org Git - sonarqube.git/blob
95e6b22f5253ac5c959224c84cc0b5c70f2697df
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2016 SonarSource SA
4  * mailto:contact AT sonarsource DOT com
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 3 of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19  */
20 package org.sonar.server.authentication.event;
21
22 import org.junit.Rule;
23 import org.junit.Test;
24 import org.junit.rules.ExpectedException;
25
26 import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
27 import static org.sonar.server.authentication.event.AuthenticationEvent.Method;
28 import static org.sonar.server.authentication.event.AuthenticationEvent.Provider;
29 import static org.sonar.server.authentication.event.AuthenticationEvent.Source;
30
31 public class AuthenticationEventSourceTest {
32   @Rule
33   public ExpectedException expectedException = ExpectedException.none();
34
35   @Test
36   public void local_fails_with_NPE_if_method_is_null() {
37     expectedException.expect(NullPointerException.class);
38     expectedException.expectMessage("method can't be null");
39
40     Source.local(null);
41   }
42
43   @Test
44   public void local_creates_source_instance_with_specified_method_and_hardcoded_provider_and_provider_name() {
45     Source underTest = Source.local(Method.BASIC_TOKEN);
46
47     assertThat(underTest.getMethod()).isEqualTo(Method.BASIC_TOKEN);
48     assertThat(underTest.getProvider()).isEqualTo(Provider.LOCAL);
49     assertThat(underTest.getProviderName()).isEqualTo("local");
50   }
51
52   @Test
53   public void oauth2_fails_with_NPE_if_providerName_is_null() {
54     expectedException.expect(NullPointerException.class);
55     expectedException.expectMessage("provider name can't be null");
56
57     Source.oauth2(null);
58   }
59
60   @Test
61   public void oauth2_creates_source_instance_with_specified_provider_name_and_hardcoded_provider_and_method() {
62     Source underTest = Source.oauth2("some name");
63
64     assertThat(underTest.getMethod()).isEqualTo(Method.OAUTH2);
65     assertThat(underTest.getProvider()).isEqualTo(Provider.EXTERNAL);
66     assertThat(underTest.getProviderName()).isEqualTo("some name");
67   }
68
69   @Test
70   public void oauth2_fails_with_IAE_if_providerName_is_empty() {
71     expectedException.expect(IllegalArgumentException.class);
72     expectedException.expectMessage("provider name can't be empty");
73
74     Source.oauth2("");
75   }
76
77   @Test
78   public void realm_fails_with_NPE_if_method_is_null() {
79     expectedException.expect(NullPointerException.class);
80     expectedException.expectMessage("method can't be null");
81
82     Source.realm(null, "name");
83   }
84
85   @Test
86   public void realm_fails_with_NPE_if_providerName_is_null() {
87     expectedException.expect(NullPointerException.class);
88     expectedException.expectMessage("provider name can't be null");
89
90     Source.realm(Method.BASIC, null);
91   }
92
93   @Test
94   public void realm_fails_with_IAE_if_providerName_is_empty() {
95     expectedException.expect(IllegalArgumentException.class);
96     expectedException.expectMessage("provider name can't be empty");
97
98     Source.realm(Method.BASIC, "");
99   }
100
101   @Test
102   public void realm_creates_source_instance_with_specified_method_and_provider_name_and_hardcoded_provider() {
103     Source underTest = Source.realm(Method.BASIC, "some name");
104
105     assertThat(underTest.getMethod()).isEqualTo(Method.BASIC);
106     assertThat(underTest.getProvider()).isEqualTo(Provider.REALM);
107     assertThat(underTest.getProviderName()).isEqualTo("some name");
108   }
109
110   @Test
111   public void sso_returns_source_instance_with_hardcoded_method_provider_and_providerName() {
112     Source underTest = Source.sso();
113
114     assertThat(underTest.getMethod()).isEqualTo(Method.SSO);
115     assertThat(underTest.getProvider()).isEqualTo(Provider.SSO);
116     assertThat(underTest.getProviderName()).isEqualTo("sso");
117
118     assertThat(underTest).isSameAs(Source.sso());
119   }
120 }