Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[improve] add AccountServiceImpl unit test #2501

Merged
merged 1 commit into from
Aug 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,138 @@

package org.apache.hertzbeat.manager.service;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.naming.AuthenticationException;

import com.usthe.sureness.provider.DefaultAccount;
import com.usthe.sureness.provider.SurenessAccount;
import com.usthe.sureness.provider.SurenessAccountProvider;
import com.usthe.sureness.provider.ducument.DocumentAccountProvider;
import com.usthe.sureness.util.JsonWebTokenUtil;
import com.usthe.sureness.util.Md5Util;
import io.jsonwebtoken.MalformedJwtException;
import org.apache.hertzbeat.common.util.JsonUtil;
import org.apache.hertzbeat.manager.pojo.dto.LoginDto;
import org.apache.hertzbeat.manager.pojo.dto.RefreshTokenResponse;
import org.apache.hertzbeat.manager.service.impl.AccountServiceImpl;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
* test case for {@link AccountServiceImpl}
*/

class AccountServiceTest {

private AccountServiceImpl accountService;

private SurenessAccountProvider accountProvider;

private final String identifier = "admin";
private final String password = "hertzbeat";
private final String salt = "salt1";
private final List<String> roles = List.of("admin");

private final String jwt = """
CyaFv0bwq2Eik0jdrKUtsA6bx3sDJeFV643R
LnfKefTjsIfJLBa2YkhEqEGtcHDTNe4CU6+9
8tVt4bisXQ13rbN0oxhUZR73M6EByXIO+SV5
dKhaX0csgOCTlCxq20yhmUea6H6JIpSE2Rwp
""";
@BeforeEach
void setUp() {

accountProvider = mock(DocumentAccountProvider.class);
accountService = new AccountServiceImpl();

JsonWebTokenUtil.setDefaultSecretKey(jwt);
}

@Test
void testAuthGetTokenWithValidAccount() throws AuthenticationException {

SurenessAccount account = DefaultAccount.builder("app1")
.setPassword(Md5Util.md5(password + salt))
.setSalt(salt)
.setOwnRoles(roles)
.setDisabledAccount(Boolean.FALSE)
.setExcessiveAttempts(Boolean.FALSE)
.build();
LoginDto loginDto = LoginDto.builder()
.credential(password)
.identifier(identifier)
.build();

when(accountProvider.loadAccount(identifier)).thenReturn(account);

Map<String, String> response = accountService.authGetToken(loginDto);

assertNotNull(response);
assertNotNull(response.get("token"));
assertNotNull(response.get("refreshToken"));
assertNotNull(response.get("role"));
assertEquals(JsonUtil.toJson(roles), response.get("role"));

}

@Test
void testAuthGetTokenWithInvalidAccount() {

String identifier = "user1";
String password = "wrongPassword";
LoginDto loginDto = LoginDto.builder()
.credential(password)
.identifier(identifier)
.build();

when(accountProvider.loadAccount(identifier)).thenReturn(null);

Assertions.assertThrows(
AuthenticationException.class,
() -> accountService.authGetToken(loginDto)
);
}

@Test
void testRefreshTokenWithValidToken() throws AuthenticationException {

String userId = "admin";
String refreshToken = JsonWebTokenUtil.issueJwt(userId, 3600L, Collections.singletonMap("refresh", true));

SurenessAccount account = DefaultAccount.builder("app1")
.setPassword(Md5Util.md5(password + salt))
.setSalt(salt)
.setOwnRoles(roles)
.setDisabledAccount(Boolean.FALSE)
.setExcessiveAttempts(Boolean.FALSE)
.build();
when(accountProvider.loadAccount(userId)).thenReturn(account);

RefreshTokenResponse response = accountService.refreshToken(refreshToken);

assertNotNull(response);
assertNotNull(response.getToken());
assertNotNull(response.getRefreshToken());
}

@Test
void testRefreshTokenWithInvalidToken() {

String refreshToken = "invalidToken";

Assertions.assertThrows(
MalformedJwtException.class,
() -> accountService.refreshToken(refreshToken)
);
}

}
Loading