Esempio n. 1
0
    def test_put_create_user(self):
        with self.app() as c:
            with self.app_context():

                Role(name="User").save_to_db()
                Role(name="Moderator").save_to_db()

                r = c.put("/user",
                          headers={"Content-Type": "application/json"},
                          data=json.dumps({
                              "name": "Test",
                              "password": "******",
                              "roles": ["User", "Moderator"]
                          }))
                self.assertEqual(r.status_code, 201)
                self.assertIsNotNone(UserModel.find_by_name("Test"))
                self.assertEqual(json.loads(r.data),
                                 {"message": "User created"})
                self.assertEqual(
                    UserModel.find_by_name("Test").roles, [
                        Role.find_by_name("User"),
                        Role.find_by_name("Moderator")
                    ])
Esempio n. 2
0
    def put(self):
        self.parser.add_argument("roles", action='append')
        data = User.parser.parse_args(strict=True)
        user = UserModel.find_by_name(data["name"])
        if user:
            user.name = data["name"]
            user.password = data["password"]
            user.roles = [
                Role.find_by_name(x) for x in data["roles"]
                if Role.find_by_name(x) != None
            ]

            user.save_to_db()

            return {"message": "User updated"}, 200
        else:
            try:
                UserModel(data["name"], data["password"],
                          [Role.find_by_name(x)
                           for x in data["roles"]]).save_to_db()

            except:
                return {"message": "An error occured"}, 500
            return {"message": "User created"}, 201