committed by
GitHub
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 318 additions and 10 deletions
-
26pom.xml
-
44src/main/java/org/keycloak/social/discord/DiscordIdentityProvider.java
-
23src/main/java/org/keycloak/social/discord/DiscordIdentityProviderConfig.java
-
68src/test/java/org/keycloak/social/discord/DiscordIdentityProviderConfigTest.java
-
18src/test/java/org/keycloak/social/discord/DiscordIdentityProviderFactoryTest.java
-
133src/test/java/org/keycloak/social/discord/DiscordIdentityProviderTest.java
-
16src/test/java/org/keycloak/social/discord/DiscordUserAttributeMapperTest.java
@ -0,0 +1,68 @@ |
|||||
|
package org.keycloak.social.discord; |
||||
|
|
||||
|
import org.junit.jupiter.api.Test; |
||||
|
import org.keycloak.models.IdentityProviderModel; |
||||
|
|
||||
|
import java.util.List; |
||||
|
|
||||
|
import static org.junit.jupiter.api.Assertions.*; |
||||
|
|
||||
|
class DiscordIdentityProviderConfigTest { |
||||
|
|
||||
|
@Test |
||||
|
void given_noAllowedGuilds_expect_emptyResult() { |
||||
|
IdentityProviderModel model = new IdentityProviderModel(); |
||||
|
DiscordIdentityProviderConfig config = new DiscordIdentityProviderConfig(model); |
||||
|
|
||||
|
assertFalse(config.hasAllowedGuilds()); |
||||
|
|
||||
|
config.setAllowedGuilds(""); |
||||
|
assertFalse(config.hasAllowedGuilds()); |
||||
|
assertTrue(config.getAllowedGuildsAsSet().isEmpty()); |
||||
|
|
||||
|
config.setAllowedGuilds(" "); |
||||
|
assertFalse(config.hasAllowedGuilds()); |
||||
|
assertTrue(config.getAllowedGuildsAsSet().isEmpty()); |
||||
|
|
||||
|
config.setAllowedGuilds(",,,"); |
||||
|
assertFalse(config.hasAllowedGuilds()); |
||||
|
assertTrue(config.getAllowedGuildsAsSet().isEmpty()); |
||||
|
|
||||
|
config.setAllowedGuilds(", ,, , ,"); |
||||
|
assertFalse(config.hasAllowedGuilds()); |
||||
|
assertTrue(config.getAllowedGuildsAsSet().isEmpty()); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
void given_allowedGuildsPresent_expect_results() { |
||||
|
List<String> guilds = List.of("0123456789123456789", "9876543210987654321"); |
||||
|
String guildsAsString = "0123456789123456789,9876543210987654321"; |
||||
|
|
||||
|
IdentityProviderModel model = new IdentityProviderModel(); |
||||
|
DiscordIdentityProviderConfig config = new DiscordIdentityProviderConfig(model); |
||||
|
|
||||
|
String expectedGuild = guilds.get(0); |
||||
|
config.setAllowedGuilds(expectedGuild); |
||||
|
assertTrue(config.hasAllowedGuilds()); |
||||
|
assertEquals(1, config.getAllowedGuildsAsSet().size()); |
||||
|
assertTrue(config.getAllowedGuildsAsSet().contains(expectedGuild)); |
||||
|
assertEquals(expectedGuild, config.getAllowedGuilds()); |
||||
|
|
||||
|
config.setAllowedGuilds("," + expectedGuild + ",,"); |
||||
|
assertTrue(config.hasAllowedGuilds()); |
||||
|
assertEquals(1, config.getAllowedGuildsAsSet().size()); |
||||
|
assertTrue(config.getAllowedGuildsAsSet().contains(expectedGuild)); |
||||
|
|
||||
|
config.setAllowedGuilds(String.join(",", guilds)); |
||||
|
assertTrue(config.hasAllowedGuilds()); |
||||
|
assertEquals(guilds.size(), config.getAllowedGuildsAsSet().size()); |
||||
|
assertTrue(config.getAllowedGuildsAsSet().containsAll(guilds)); |
||||
|
assertEquals(guildsAsString, config.getAllowedGuilds()); |
||||
|
|
||||
|
config.setAllowedGuilds(String.join(", ,, , ,", guilds)); |
||||
|
assertTrue(config.hasAllowedGuilds()); |
||||
|
assertEquals(guilds.size(), config.getAllowedGuildsAsSet().size()); |
||||
|
assertTrue(config.getAllowedGuildsAsSet().containsAll(guilds)); |
||||
|
assertEquals(guildsAsString, config.getAllowedGuilds()); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,18 @@ |
|||||
|
package org.keycloak.social.discord; |
||||
|
|
||||
|
import org.junit.jupiter.api.Test; |
||||
|
|
||||
|
import static org.junit.jupiter.api.Assertions.*; |
||||
|
|
||||
|
class DiscordIdentityProviderFactoryTest { |
||||
|
|
||||
|
@Test |
||||
|
void getName() { |
||||
|
DiscordIdentityProviderFactory factory = new DiscordIdentityProviderFactory(); |
||||
|
|
||||
|
String name = factory.getName(); |
||||
|
assertNotNull(name); |
||||
|
assertNotEquals(0, name.length()); |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,133 @@ |
|||||
|
package org.keycloak.social.discord; |
||||
|
|
||||
|
import com.fasterxml.jackson.databind.JsonNode; |
||||
|
import com.fasterxml.jackson.databind.ObjectMapper; |
||||
|
import org.junit.jupiter.api.BeforeEach; |
||||
|
import org.junit.jupiter.api.Test; |
||||
|
import org.junit.jupiter.api.extension.ExtendWith; |
||||
|
import org.keycloak.broker.provider.BrokeredIdentityContext; |
||||
|
import org.keycloak.models.KeycloakSession; |
||||
|
import org.mockito.Mock; |
||||
|
import org.mockito.junit.jupiter.MockitoExtension; |
||||
|
|
||||
|
import static org.junit.jupiter.api.Assertions.assertEquals; |
||||
|
import static org.junit.jupiter.api.Assertions.assertNull; |
||||
|
import static org.mockito.Mockito.when; |
||||
|
|
||||
|
@ExtendWith(MockitoExtension.class) |
||||
|
class DiscordIdentityProviderTest { |
||||
|
|
||||
|
DiscordIdentityProvider provider; |
||||
|
|
||||
|
ObjectMapper mapper; |
||||
|
|
||||
|
@Mock |
||||
|
KeycloakSession session; |
||||
|
|
||||
|
@Mock |
||||
|
DiscordIdentityProviderConfig config; |
||||
|
|
||||
|
@BeforeEach |
||||
|
void setUp() { |
||||
|
when(config.getAlias()).thenReturn("discord"); |
||||
|
|
||||
|
provider = new DiscordIdentityProvider(session, config); |
||||
|
|
||||
|
mapper = new ObjectMapper(); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
void testNameDiscriminatorHandling() throws Exception { |
||||
|
String jsonProfileLegacy = """ |
||||
|
{ |
||||
|
"id": "80351110224678912", |
||||
|
"username": "Nelly", |
||||
|
"discriminator": "1337", |
||||
|
"email": "nelly@discord.com" |
||||
|
} |
||||
|
"""; |
||||
|
JsonNode profile = mapper.readTree(jsonProfileLegacy); |
||||
|
BrokeredIdentityContext user = provider.extractIdentityFromProfile(null, profile); |
||||
|
|
||||
|
assertEquals("80351110224678912", user.getId()); |
||||
|
assertEquals("nelly#1337", user.getUsername()); |
||||
|
|
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
void testNameDiscriminatorHandling_NoDiscriminator() throws Exception { |
||||
|
String jsonProfileNew = """ |
||||
|
{ |
||||
|
"id": "80351110224678912", |
||||
|
"username": "nelly", |
||||
|
"discriminator": "0", |
||||
|
"email": "nelly@discord.com" |
||||
|
} |
||||
|
"""; |
||||
|
JsonNode profile = mapper.readTree(jsonProfileNew); |
||||
|
BrokeredIdentityContext user = provider.extractIdentityFromProfile(null, profile); |
||||
|
|
||||
|
assertEquals("80351110224678912", user.getId()); |
||||
|
assertEquals("nelly", user.getUsername()); |
||||
|
assertEquals("nelly@discord.com", user.getEmail()); |
||||
|
|
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
void testExtractIdentity_NoAvatar() throws Exception { |
||||
|
String jsonProfile = """ |
||||
|
{ |
||||
|
"id": "80351110224678912", |
||||
|
"username": "nelly", |
||||
|
"discriminator": "0", |
||||
|
"email": "nelly@discord.com" |
||||
|
} |
||||
|
"""; |
||||
|
JsonNode profile = mapper.readTree(jsonProfile); |
||||
|
|
||||
|
BrokeredIdentityContext user = provider.extractIdentityFromProfile(null, profile); |
||||
|
|
||||
|
assertNull(user.getUserAttribute("picture")); |
||||
|
|
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
void testExtractIdentity_WithStaticAvatar() throws Exception { |
||||
|
String jsonProfile = """ |
||||
|
{ |
||||
|
"id": "80351110224678912", |
||||
|
"username": "nelly", |
||||
|
"discriminator": "0", |
||||
|
"avatar": "8342729096ea3675442027381ff50dfe", |
||||
|
"email": "nelly@discord.com" |
||||
|
} |
||||
|
"""; |
||||
|
JsonNode profile = mapper.readTree(jsonProfile); |
||||
|
|
||||
|
BrokeredIdentityContext user = provider.extractIdentityFromProfile(null, profile); |
||||
|
|
||||
|
String expectedURL = "https://cdn.discordapp.com/avatars/80351110224678912/8342729096ea3675442027381ff50dfe.png?size=256"; |
||||
|
assertEquals(expectedURL, user.getUserAttribute("picture")); |
||||
|
|
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
void testExtractIdentity_WithAnimatedAvatar() throws Exception { |
||||
|
String jsonProfile = """ |
||||
|
{ |
||||
|
"id": "80351110224678912", |
||||
|
"username": "nelly", |
||||
|
"discriminator": "0", |
||||
|
"avatar": "a_8342729096ea3675442027381ff50dfe", |
||||
|
"email": "nelly@discord.com" |
||||
|
} |
||||
|
"""; |
||||
|
JsonNode profile = mapper.readTree(jsonProfile); |
||||
|
|
||||
|
BrokeredIdentityContext user = provider.extractIdentityFromProfile(null, profile); |
||||
|
|
||||
|
String expectedURL = "https://cdn.discordapp.com/avatars/80351110224678912/a_8342729096ea3675442027381ff50dfe.gif?size=256"; |
||||
|
assertEquals(expectedURL, user.getUserAttribute("picture")); |
||||
|
|
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,16 @@ |
|||||
|
package org.keycloak.social.discord; |
||||
|
|
||||
|
import org.junit.jupiter.api.Test; |
||||
|
|
||||
|
import static org.junit.jupiter.api.Assertions.*; |
||||
|
|
||||
|
class DiscordUserAttributeMapperTest { |
||||
|
@Test |
||||
|
void getCompatibleProviders() { |
||||
|
DiscordUserAttributeMapper mapper = new DiscordUserAttributeMapper(); |
||||
|
String[] providers = mapper.getCompatibleProviders(); |
||||
|
assertNotNull(providers); |
||||
|
assertNotEquals(0, providers.length); |
||||
|
} |
||||
|
|
||||
|
} |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue