java - 使用 BCrypt 加载测试数据

标签 java authentication junit playframework bcrypt

我正在使用 BCrypt 来加密密码,并且我有一个正在尝试为其编写测试的模型。我制作了一个 YAML 文件,其中包含一些基本测试数据,但我似乎无法正确获取代码。

我的帐户模型:

package models;

import javax.persistence.*;
import org.mindrot.jbcrypt.BCrypt;
import play.data.validation.Constraints.Required;
import play.db.ebean.Model;
import play.db.ebean.Model.Finder;

@Entity
public class Account extends Model {

    @Id
    public Long id;
    public String email;
    public String username;
    public String passwordhash;

    public Account(String email, String username, String password) {
        this.email = email;
        this.username = username;
        this.passwordhash = BCrypt.hashpw(password, BCrypt.gensalt()); 
    }

    public static Finder<Long, Account> find = new Finder<Long, Account>(Long.class, Account.class);

    public static Account create(
            String username, String email, String password, 
            String confirmPassword
    ) {
        Account account = new Account(username, email, password);
        account.save();
        return account;
    }

    public static Account authenticate (String username, String password) {
        Account account = Account.find.where().eq("username", username).findUnique();
        if (account != null && BCrypt.checkpw(password, account.passwordhash)) {
            return account;
        } else {
            return null;
        }
    }

}

我的模型测试:

package ModelTests;

import models.*;
import org.junit.*;
import com.avaje.ebean.Ebean;
import static org.junit.Assert.*;
import play.libs.Yaml;
import play.test.WithApplication;
import static play.test.Helpers.*;

import java.util.List;

public class AccountTest extends WithApplication {

    @Before
    public void setUp() {
        start(fakeApplication(inMemoryDatabase()));
        Ebean.save((List) Yaml.load("test-data.yml"));
    }

    @Test
    public void authentication() {
        assertNotNull(Account.authenticate("palug", "Oxford4EVA"));
        assertNotNull(Account.authenticate("isthatastrawhat", "Supernatural"));
        assertNull(Account.authenticate("MelvIsntNormal", "meaplesss"));
        assertNull(Account.authenticate("SomeStupidHacker", "dogewow"));
    }

}

以及我的测试数据 YAML:

# Accounts

- &melvin !!models.Account
    email: mxtcapps@maxtrace.co.uk
    username: MelvIsntNormal
    password: HashedPassword

- &emma !!models.Account
    email: isthatastrawhat@maxtrace.co.uk
    username: isthatastrawhat
    password: Supernatural

- &paul !!models.Account
    email: palug@maxtrace.co.uk
    username: palug
    password: Oxford4EVA

当我运行测试时,出现此错误:

[error] Test ModelTests.AccountTest.authentication failed: org.yaml.snakeyaml.constructor.ConstructorException: null; Can't construct a java object for tag:yaml.org,2002:models.Account; exception=Cannot create property=password for JavaBean=models.Account@1c8d2d9; Unable to find property 'password' on class: models.Account;  in 'reader', line 3, column 3:
[error]     - &melvin !!models.Account
[error]       ^
[error]     at org.yaml.snakeyaml.constructor.Constructor$ConstructYamlObject.construct(Constructor.java:333)
[error]     at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:182)
[error]     at org.yaml.snakeyaml.constructor.BaseConstructor.constructSequenceStep2(BaseConstructor.java:276)
[error]     at org.yaml.snakeyaml.constructor.BaseConstructor.constructSequence(BaseConstructor.java:247)
[error]     at org.yaml.snakeyaml.constructor.SafeConstructor$ConstructYamlSeq.construct(SafeConstructor.java:442)
[error]     at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:182)
[error]     at org.yaml.snakeyaml.constructor.BaseConstructor.constructDocument(BaseConstructor.java:141)
[error]     at org.yaml.snakeyaml.constructor.BaseConstructor.getSingleData(BaseConstructor.java:127)
[error]     at org.yaml.snakeyaml.Yaml.loadFromReader(Yaml.java:481)
[error]     at org.yaml.snakeyaml.Yaml.load(Yaml.java:412)
[error]     at play.libs.Yaml.load(Yaml.java:31)
[error]     at play.libs.Yaml.load(Yaml.java:18)
[error]     at ModelTests.AccountTest.setUp(AccountTest.java:18)
[error]     ...
[error] Caused by: org.yaml.snakeyaml.error.YAMLException: Cannot create property=password for JavaBean=models.Account@1c8d2d9; Unable to find property 'password' on class: models.Account
[error]     at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.constructJavaBean2ndStep(Constructor.java:299)
[error]     at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.construct(Constructor.java:189)
[error]     at org.yaml.snakeyaml.constructor.Constructor$ConstructYamlObject.construct(Constructor.java:331)
[error]     ... 48 more
[error] Caused by: org.yaml.snakeyaml.error.YAMLException: Unable to find property 'password' on class: models.Account
[error]     at org.yaml.snakeyaml.introspector.PropertyUtils.getProperty(PropertyUtils.java:132)
[error]     at org.yaml.snakeyaml.introspector.PropertyUtils.getProperty(PropertyUtils.java:121)
[error]     at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.getProperty(Constructor.java:308)
[error]     at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.constructJavaBean2ndStep(Constructor.java:240)
[error]     ... 50 more
[info] ModelTests.AccountTest
[info] x authentication
[info] 
[info] 
[info] Total for test ModelTests.AccountTest
[info] Finished in 0.039 seconds
[info] 1 tests, 1 failures, 0 errors
[error] Failed: Total 1, Failed 1, Errors 0, Passed 0
[error] Failed tests:
[error]     ModelTests.AccountTest
[error] (test:test) sbt.TestsFailedException: Tests unsuccessful
[error] Total time: 26 s, completed 01-Feb-2014 07:59:02

我了解该错误是由于密码字段不存在而引起的。但是,我不太确定如何进行测试来调用 Account 方法,以便对密码进行加密。谁能帮忙解决这个问题吗?

问题解答(感谢@Joe)

我必须为密码字段添加一个设置方法:

public void setPassword(String password) {
        this.passwordhash = BCrypt.hashpw(password, BCrypt.gensalt());
}

我的Account模型现在看起来像这样: 封装模型;

import javax.persistence.*;
import org.mindrot.jbcrypt.BCrypt;
import play.data.validation.Constraints.Required;
import play.db.ebean.Model;
import play.db.ebean.Model.Finder;

@Entity
public class Account extends Model {

    @Id
    public Long id;
    public String email;
    public String username;
    public String passwordhash;

    public Account(String email, String username, String password) {
        this.email = email;
        this.username = username;
        this.passwordhash = BCrypt.hashpw(password, BCrypt.gensalt()); 
    }

    public static Finder<Long, Account> find = new Finder<Long, Account>(Long.class, Account.class);

    public static Account create(
            String username, String email, String password, 
            String confirmPassword
    ) {
        Account account = new Account(username, email, password);
        account.save();
        return account;
    }

    public void setPassword(String password) {
        this.passwordhash = BCrypt.hashpw(password, BCrypt.gensalt());
    }

    public static Account authenticate (String username, String password) {
        Account account = Account.find.where().eq("username", username).findUnique();
        if (account != null && BCrypt.checkpw(password, account.passwordhash)) {
            return account;
        } else {
            return null;
        }
    }

}

最佳答案

该错误表明您的 models.Account 类没有 password 属性。 SnakeYAML 正在寻找 either a public field called password or a JavaBeans setter method called setPassword :

By default standard JavaBean properties and public fields are included.

两者都没有找到:

[error] Caused by: org.yaml.snakeyaml.error.YAMLException: Unable to find property 'password' on class: models.Account

相反,它有一个 passwordHash 字段。这表明有两种可能性:

  1. 在测试数据中定义 passwordHash
  2. models.Account 上定义一个 setPassword(String) 方法,该方法将对密码进行哈希处理并将其存储在 passwordHash 中。

测试应该做一件事,所以我建议做第一件事。您正在针对已知密码测试身份验证。将哈希定义为测试的一部分意味着您可以在测试数据中查看已知密码的哈希,并避免在遇到错误时通过的测试(例如,hashpw 方法始终返回空字符串。

关于java - 使用 BCrypt 加载测试数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21509812/

相关文章:

wordpress 获取当前用户的登录 session 数

java - JUnit hibernate : testing lazy associations fetched

java - Ant构建文件问题

java - "xmlns:log4j"是 <log4j :configuration> tag? 的必需属性吗

java - 对象未使用 PageFactory.initElements(driver,class); 进行初始化

java - 使用 Java 驱动程序在 Mongodb 中出现超时异常

php - Laravel 4 - 如何为从移动应用程序访问的 REST API 实现自定义身份验证提供程序?

java - 登录playframework后的页面

java.lang.SecurityException : class "org.eclipse.core.runtime.PlatformObject"'s signer information does not match signer information 错误

java - 集群 ehcache 的 Junit 测试策略