joscha / play-authenticate

An authentication plugin for Play Framework 2.x (Java)
http://joscha.github.com/play-authenticate/
Other
807 stars 367 forks source link

Injecting sample users for testing #230

Closed slimandslam closed 9 years ago

slimandslam commented 9 years ago

Hi Joscha, Do you have a recommended way of injecting some sample users into a Play app with play-authenticate to make testing easy? I'd like to have a few sample users in the database of the Play app and set smtp.mock=true to do testing on our staging site.

-J

joscha commented 9 years ago

Hi @slimandslam,

you can use https://github.com/joscha/play-authenticate/blob/master/samples/java/play-authenticate-usage/app/Global.java#L73 together with a little snippet:

        public static void insert(Application app) {
            final boolean noRoles = Ebean.find(SecurityRole.class).findRowCount() == 0;
            final boolean noUsers = Ebean.find(User.class).findRowCount() == 0;

            if (noRoles || noUsers) {
                @SuppressWarnings("unchecked")
                final Map<String, List<Object>> all = (Map<String, List<Object>>) Yaml.load("initial-data.yml");

                try {
                    if (noRoles) {
                        Ebean.save(all.get("roles"));
                    }

                    if (noUsers) {
                        // Insert users first
                        Ebean.save(all.get("users"));
                        for (final Object user : all.get("users")) {
                            // Insert the User/SecurityRole relation
                            Ebean.saveManyToManyAssociations(user, "roles");
                        }
                    }
                } catch (com.avaje.ebean.ValidationException ex) {
                    Logger.error(ex.getInvalid().toString());
                    throw ex;
                }
            }
        }
    }

where initial-data.yml (in conf) is

# Security roles

roles:
    - !!models.SecurityRole
        id:             1
        roleName:       user
    - !!models.SecurityRole
        id:             2
        roleName:       admin

# Users

users:
    - !!models.User
        email:          some@admin.com
        name:           An Admin
        active:         true
        emailValidated: true
        linkedAccounts:
                 - !!models.LinkedAccount
                     providerUserId: "1122334455667788"
                     providerKey: "google"
        roles:
                - !!models.SecurityRole
                    id:       1
                - !!models.SecurityRole
                    id:       2

    - !!models.User
        email:          other@user.com
        name:           A User
        active:         true
        emailValidated: true
        roles:
                - !!models.SecurityRole
                    id:       1
joscha commented 9 years ago

@slimandslam we could actually make this part of the sample application if you would feel comfortable composing a PR :)

slimandslam commented 9 years ago

Joscha, if one is using username/password auth, it's not clear how to inject those users. If I put a plaintext password into the yml file, e.g.

`linkedAccounts:

It never gets encrypted.

joscha commented 9 years ago

The password provider is somewhat special in that respect - I don't think I ever tried doing that out of the box, so I think you'd either need to adapt the insert method to encrypt on the fly (before inserting) oder add the encrypted passwords into the yml file (which is probably better anyway, as otherwise there would be cleartext passwords in your source code).

slimandslam commented 9 years ago

I just added the encrypted passwords manually into the yml file. It works!

J

On Fri, Apr 3, 2015 at 2:45 AM, Joscha Feth notifications@github.com wrote:

The password provider is somewhat special in that respect - I don't think I ever tried doing that out of the box, so I think you'd either need to adapt the insert method to encrypt on the fly (before inserting) oder add the encrypted passwords into the yml file (which is probably better anyway, as otherwise there would be cleartext passwords in your source code).

— Reply to this email directly or view it on GitHub https://github.com/joscha/play-authenticate/issues/230#issuecomment-89207099 .