I think I found a bug in register_bare()

56 views
Skip to first unread message

Richard

unread,
Mar 31, 2016, 12:57:28 PM3/31/16
to web2py-developers
Hello,

Please have a look at this PR :


test_register_bare() test case... I can't get it to pass if I don't change :

    def register_bare(self, **fields):
        """
        Registers a user as specified by username (or email)
        and a raw password.
        """
        settings = self._get_login_settings()
        # users can register_bare even if no password is provided,
        # in this case they will have to reset their password to login
        if fields.get(settings.passfield):
            fields[settings.passfield] = \
                settings.table_user[settings.passfield].validate(fields[settings.passfield])[0]
        if not fields.get(settings.userfield):
            raise ValueError('register_bare: ' +
                             'userfield not provided or invalid')
        user = self.get_or_create_user(fields, login=False, get=False,
                                       update_fields=self.settings.update_fields)
        if not user:
            # get or create did not create a user (it ignores duplicate records)
            return False
        return user


I think that the issue is coming from this line :

user = self.get_or_create_user(fields, login=False, get=False,
                                       update_fields=self.settings.update_fields)

Which should be "get=True" or it make no sens or the register_bare method always return False...

What do you think?

Please don't merge the PR, until I complete the trouble shooting and submit a final PR

Thanks

Richard


Leonel Câmara

unread,
Mar 31, 2016, 1:27:23 PM3/31/16
to web2py-developers
No No. register_bare doesn't always return False. 

get=False is important because if you get_or_create_user returns None you know that you are possibly trying to add the same user twice.

Basically register_bare wants to use get_or_create_user as if it was just create_user, a create_user would return False if the user already existed, it would not return the user it did not create.

If you register a user that is not in the database register_bare should return it.

I'm thinking your problem is that you forgot to truncate in the end of test_register_bare so the user is in fact there.

Richard Vézina

unread,
Mar 31, 2016, 1:31:20 PM3/31/16
to web2py-d...@googlegroups.com
Yeah I realize after that there were particular things for register_bare in get_or_create_user...

--
-- mail from:GoogleGroups "web2py-developers" mailing list
make speech: web2py-d...@googlegroups.com
unsubscribe: web2py-develop...@googlegroups.com
details : http://groups.google.com/group/web2py-developers
the project: http://code.google.com/p/web2py/
official : http://www.web2py.com/
---
You received this message because you are subscribed to the Google Groups "web2py-developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py-develop...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Richard Vézina

unread,
Mar 31, 2016, 2:19:16 PM3/31/16
to web2py-d...@googlegroups.com
I feel that there something missing in register_bare to build a proper test case though... I mean if I received False either if it succeed in register or not we can make the assertion that we had register the user... And to me it make no sens that we create the user at the same time we register it, I mean there may situation it make sens in real life but from testing point of view, we should be able to assert both...

Richard

Leonel Câmara

unread,
Mar 31, 2016, 2:24:40 PM3/31/16
to web2py-developers
What do you mean? You can test that it returns a correct user when you register_bare, then you can test that it returns False if you try to register that same user again, finally you can try to register a user with some value that doesn't validate and assert that it raises a ValueError

Richard Vézina

unread,
Mar 31, 2016, 2:35:49 PM3/31/16
to web2py-d...@googlegroups.com
ValueError I am ok... I may not understand something... Bassically I thought that you register a user already defined in auth_user so registration_key get updated from something to nothing '' then the user is allowed to login... I my assomption is rigth I can test this test case actually...

This all pass :

def test_register_bare(self):
        self.db.auth_user.insert(first_name='Bart',
                                 last_name='Simpson',
                                 username='user1',
                                 email='us...@test.com',
                                 password='password_123',
                                 registration_key=None,
                                 registration_id=None)
        self.db.commit()
        # failing register_bare
        self.assertEqual(self.auth.register_bare(username='user1', password='wrong_password'), False)
        # successful register_bare
        self.assertEqual(self.auth.register_bare(username='user2',
                                                 email='us...@test.com',
                                                 password='password_123')['id'], 2)
        # raise ValueError
        self.assertRaises(ValueError, self.auth.register_bare,
                          **dict(wrong_field_name='user1', password='password_123'))

But is user2 bare registration really a registration? I mean it create the user at the same time from what you explained...

Thanks to help me understand...

Richard

On Thu, Mar 31, 2016 at 2:24 PM, Leonel Câmara <leonel...@gmail.com> wrote:
What do you mean? You can test that it returns a correct user when you register_bare, then you can test that it returns False if you try to register that same user again, finally you can try to register a user with some value that doesn't validate and assert that it raises a ValueError

--

Leonel Câmara

unread,
Mar 31, 2016, 2:41:22 PM3/31/16
to web2py-d...@googlegroups.com
I don't get it, what do you mean a "real registration"? Of course it is, it's inserted in the auth_user database table, what more is there left for a registration to be real?

Those tests seem fine to me if you add a truncate in the end and maybe a test with an invalid email or something also throwing a ValueError.

Richard Vézina

unread,
Mar 31, 2016, 3:55:55 PM3/31/16
to web2py-d...@googlegroups.com
So I may be confused... I may refer to confirmation... I thought of registration as a confirmation of the initial sign up when you wait for email then you authenticate with a token...

Thanks...

Richard

On Thu, Mar 31, 2016 at 2:41 PM, Leonel Câmara <leonel...@gmail.com> wrote:
I don't get it, what do you mean a "real registration"? Of course it is, it's inserted in the auth_user database, what more is there left for a registration to be real?

Those tests seem fine to me if you add a truncate in the end and maybe a test with an invalid email or something also throwing a ValueError.

--

Leonel Câmara

unread,
Mar 31, 2016, 4:01:10 PM3/31/16
to web2py-d...@googlegroups.com
No registration_bare, is exactly like regular registration, it's only bare in the sense that you don't have a SQLFORM and controller code provided for you like in the regular auth.register() registration keys have nothing to do with any of this.
Reply all
Reply to author
Forward
0 new messages