def setUp(self):
        super(TestUserMerge, self).setUp()
        user1 = User.query.filter_by(username=u'user1').one()
        user2 = User.query.filter_by(username=u'user2').one()
        user2.merge_into(user1)
        # Normally this would be done by Lastuser:
        user1.userinfo[u'oldids'] = [user2.userid]
        db.session.commit()

        # Update profiles
        Profile.update_from_user(user1, db.session)

        self.user1 = User.query.filter_by(userid=u"1234567890123456789012").one()
        self.user2 = User.query.filter_by(userid=u"0987654321098765432109").one()
        self.user3 = User.query.filter_by(userid=u"1234567890987654321234").one()

        self.team1 = Team.query.filter_by(userid=u"1324354657687980089786").one()
        self.team2 = Team.query.filter_by(userid=u"0897867564534231243546").one()
        self.team3 = Team.query.filter_by(userid=u"1324354657687980132435").one()
    def setUp(self):
        super(TestUserMerge, self).setUp()
        user1 = User.query.filter_by(username=u'user1').one()
        user2 = User.query.filter_by(username=u'user2').one()
        user2.merge_into(user1)
        # Normally this would be done by Lastuser:
        user1.userinfo[u'oldids'] = [user2.userid]
        db.session.commit()

        # Update profiles
        Profile.update_from_user(user1, db.session)

        self.user1 = User.query.filter_by(userid=u"1234567890123456789012").one()
        self.user2 = User.query.filter_by(userid=u"0987654321098765432109").one()
        self.user3 = User.query.filter_by(userid=u"1234567890987654321234").one()

        self.team1 = Team.query.filter_by(userid=u"1324354657687980089786").one()
        self.team2 = Team.query.filter_by(userid=u"0897867564534231243546").one()
        self.team3 = Team.query.filter_by(userid=u"1324354657687980132435").one()

        def request_callback(request, uri, headers):
            if 'userid' in request.parsed_body and request.parsed_body['userid'][0] == '0987654321098765432109':
                response = {
                    "status": "ok",
                    "type": "user",
                    "buid": "1234567890123456789012",
                    "userid": "1234567890123456789012",
                    "name": "user1",
                    "title": "User 1",
                    "label": "User 1 (~user1)",
                    "oldids": ['0987654321098765432109'],
                    "timezone": "Asia/Kolkata",
                    }
            else:
                response = {'status': 'error', 'error': 'not_found'}
            return (200, headers, json.dumps(response))

        httpretty.enable()
        httpretty.register_uri(httpretty.POST, self.lastuser.endpoint_url(self.lastuser.getuser_userid_endpoint),
            body=request_callback,
            content_type="application/json")
    def create_test_data(self):
        user1 = User(userid=u"1234567890123456789012", username=u"user1", fullname=u"User 1",
            email=u'*****@*****.**',
            userinfo={
                u'timezone': u'Asia/Kolkata',
                u'organizations': {
                    u'member': [
                        {
                            u'userid': u'qazwsxedcrfvtgbyhnujmi',
                            u'name': u'org1',
                            u'title': u'Organization 1',
                            },
                        ],
                    u'owner': [
                        {
                            u'userid': u'qazwsxedcrfvtgbyhnujmi',
                            u'name': u'org1',
                            u'title': u'Organization 1',
                            },
                        ]
                    }
                })
        user2 = User(userid=u"0987654321098765432109", username=u"user2", fullname=u"User 2",
            email=u'*****@*****.**',
            userinfo={
                u'timezone': u'Asia/Kolkata',
                u'organizations': {
                    u'member': [
                        {
                            u'userid': u'qwertyuiopasdfghjklzxc',
                            u'name': u'org2',
                            u'title': u'Organization 2',
                            },
                        {
                            u'userid': u'mnbvcxzlkjhgfdsapoiuyt',
                            u'name': u'org3',
                            u'title': u'Organization 3',
                            },
                        ],
                    u'owner': [
                        {
                            u'userid': u'qwertyuiopasdfghjklzxc',
                            u'name': u'org2',
                            u'title': u'Organization 2',
                            },
                        {
                            'userid': 'mnbvcxzlkjhgfdsapoiuyt',
                            'name': 'org3',
                            'title': 'Organization 3',
                            },
                        ]
                    }
                })
        user3 = User(userid=u"1234567890987654321234", username=u"user3", fullname=u"User 3",
            email=u'*****@*****.**',
            userinfo={
                u'timezone': 'Asia/Kolkata',
                u'organizations': {
                    u'member': [
                        {
                            u'userid': u'mnbvcxzlkjhgfdsapoiuyt',
                            u'name': u'org3',
                            u'title': u'Organization 3',
                            },
                        {
                            u'userid': u'qazwsxedcrfvtgbyhnujmi',
                            u'name': u'org1',
                            u'title': u'Organization 1',
                            },
                        ],
                    u'owner': [
                        {
                            u'userid': u'mnbvcxzlkjhgfdsapoiuyt',
                            u'name': u'org3',
                            u'title': u'Organization 3',
                            },
                        {
                            u'userid': u'qazwsxedcrfvtgbyhnujmi',
                            u'name': u'org1',
                            u'title': u'Organization 1',
                            },
                        ]
                    }
                })

        team1 = Team(userid=u"1324354657687980089786", orgid=u"qazwsxedcrfvtgbyhnujmi",
            title=u"Team 1", users=[user1, user2])
        team2 = Team(userid=u"0897867564534231243546", orgid=u"qwertyuiopasdfghjklzxc",
            title=u"Team 2", users=[user2, user3])
        team3 = Team(userid=u"1324354657687980132435", orgid=u"mnbvcxzlkjhgfdsapoiuyt",
            title=u"Team 3", users=[user3, user1])

        db.session.add_all([user1, user2, user3, team1, team2, team3])
        db.session.flush()

        # Create six profiles (3 users + 3 orgs)
        Profile.update_from_user(user1, db.session)
        Profile.update_from_user(user2, db.session)
        Profile.update_from_user(user3, db.session)

        db.session.commit()