Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-marechal committed Mar 1, 2024
1 parent 736cbf4 commit 0f8c930
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/********************************************************************************
* Copyright (c) 2023 Ericsson and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
********************************************************************************/
package org.eclipse.openvsx.security;

import org.springframework.security.oauth2.core.user.OAuth2User;

public class DefaultAuthUser implements AuthUser {

final String authId;
final String avatarUrl;
final String email;
final String fullName;
final String loginName;
final String providerId;
final String providerUrl;

public DefaultAuthUser(String providerId, OAuth2User oauth2User) {
authId = oauth2User.getName();
avatarUrl = oauth2User.getAttribute("avatar_url");
email = oauth2User.getAttribute("email");
fullName = oauth2User.getAttribute("name");
loginName = oauth2User.getAttribute("login");
this.providerId = providerId;
providerUrl = oauth2User.getAttribute("html_url");
}

@Override
public String getAuthId() {
return authId;
}

@Override
public String getAvatarUrl() {
return avatarUrl;
}

@Override
public String getEmail() {
return email;
}

@Override
public String getFullName() {
return fullName;
}

@Override
public String getLoginName() {
return loginName;
}

@Override
public String getProviderId() {
return providerId;
}

@Override
public String getProviderUrl() {
return providerUrl;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@ public IdPrincipal loadUser(OAuth2UserRequest userRequest) {
}

private IdPrincipal loadGitHubUser(OAuth2UserRequest userRequest) {
var authUser = new GithubAuthUser(delegate.loadUser(userRequest));
var authUser = new DefaultAuthUser("github", delegate.loadUser(userRequest));
String loginName = authUser.getLoginName();
if (StringUtils.isEmpty(loginName))
throw new CodedAuthException("Invalid login: missing 'login' field.", INVALID_GITHUB_USER);
var userData = repositories.findUserByLoginName("github", loginName);
var userData = repositories.findUserByLoginName(authUser.getProviderId(), loginName);
if (userData == null) {
userData = users.registerNewUser(authUser);
} else {
Expand Down Expand Up @@ -173,31 +173,4 @@ private Collection<GrantedAuthority> getAuthorities(UserData userData) {
return Collections.emptyList();
}
}

static class GithubAuthUser implements AuthUser {

final String authId;
final String avatarUrl;
final String email;
final String fullName;
final String loginName;
final String providerUrl;

@Override public String getAuthId() { return authId; }
@Override public String getAvatarUrl() { return avatarUrl; }
@Override public String getEmail() { return email; }
@Override public String getFullName() { return fullName; }
@Override public String getLoginName() { return loginName; }
@Override public String getProviderId() { return "github"; }
@Override public String getProviderUrl() { return providerUrl; }

public GithubAuthUser(OAuth2User oauth2User) {
authId = oauth2User.getName();
avatarUrl = oauth2User.getAttribute("avatar_url");
email = oauth2User.getAttribute("email");
fullName = oauth2User.getAttribute("name");
loginName = oauth2User.getAttribute("login");
providerUrl = oauth2User.getAttribute("html_url");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,35 @@
* ****************************************************************************** */
package org.eclipse.openvsx.cache;

import static org.eclipse.openvsx.cache.CacheService.CACHE_EXTENSION_JSON;
import static org.eclipse.openvsx.entities.FileResource.DOWNLOAD;
import static org.eclipse.openvsx.entities.FileResource.STORAGE_DB;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import org.eclipse.openvsx.ExtensionService;
import org.eclipse.openvsx.LocalRegistryService;
import org.eclipse.openvsx.UserService;
import org.eclipse.openvsx.admin.AdminService;
import org.eclipse.openvsx.entities.*;
import org.eclipse.openvsx.entities.Extension;
import org.eclipse.openvsx.entities.ExtensionReview;
import org.eclipse.openvsx.entities.ExtensionVersion;
import org.eclipse.openvsx.entities.FileResource;
import org.eclipse.openvsx.entities.Namespace;
import org.eclipse.openvsx.entities.PersonalAccessToken;
import org.eclipse.openvsx.entities.UserData;
import org.eclipse.openvsx.json.ExtensionJson;
import org.eclipse.openvsx.json.ReviewJson;
import org.eclipse.openvsx.repositories.RepositoryService;
import org.eclipse.openvsx.security.DefaultAuthUser;
import org.eclipse.openvsx.security.IdPrincipal;
import org.eclipse.openvsx.util.TimeUtil;
import org.junit.jupiter.api.Test;
Expand All @@ -34,16 +55,6 @@

import jakarta.persistence.EntityManager;
import jakarta.transaction.Transactional;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import static org.eclipse.openvsx.cache.CacheService.CACHE_EXTENSION_JSON;
import static org.eclipse.openvsx.entities.FileResource.DOWNLOAD;
import static org.eclipse.openvsx.entities.FileResource.STORAGE_DB;
import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
Expand Down Expand Up @@ -113,7 +124,8 @@ public void testUpdateExistingUser() {

var user = extVersion.getPublishedWith().getUser();
var oauthUser = new DefaultOAuth2User(authorities, attributes, "name");
users.updateExistingUser(user, oauthUser);
var authUser = new DefaultAuthUser(authority, oauthUser);
users.updateExistingUser(user, authUser);
assertNull(cache.getCache(CACHE_EXTENSION_JSON).get(cacheKey, ExtensionJson.class));

var json = registry.getExtension(namespace.getName(), extension.getName(), extVersion.getTargetPlatform(), extVersion.getVersion());
Expand Down

0 comments on commit 0f8c930

Please sign in to comment.