How to use named Machinist blueprints in Cucumber with Pickle - ruby

I noticed this little tid-bit in the readme for pickle:
(The latest version of pickle supports multiple
blueprints, for earlier versions of machinist use pickle <= 0.1.10)
I'm running in to a situation where I want to create a user with admin privileges. I've got two machinist blueprints:
User.blueprint do
first_name
last_name
email
password { "password" }
password_confirmation { "password" }
state "active"
end
User.blueprint(:super_admin) do
roles { ["super-admin", "moderator"] }
end
Rather than creating a custom step for myself to create the super-admin user, I was wondering if there's a way to do it using Pickle.
Using the following will use the normal pickle step and reference the basic machinist User blueprint:
Given a user exists with first_name: "Test", last_name: "Man"
The question is, how do I say that I want a super-admin user?

Not sure how this would be done in Pickle, with the very similar gem Cucumber Factory you could create a super-admin user like this:
Given there is a user (super admin) with the first name "Test" and the last name "Man"

So it turns out that creating named Machinist blueprints in Pickle is pretty simple:
Given a super admin user exists with first_name: "Test", last_name: "Man"
This line will call the same pickle step, but Pickle is intelligent enough to map the text "super admin user" to the User.blueprint(:super_admin) factory.
It's such an obvious method that I really should have just tried it before asking. The documentation for Pickle doesn't explicitly mention the syntax for doing this anywhere, only that it's possible to do. So now we know.

Related

Pymongo/Discord.py How to add the same user for another server into the database

So, I'm trying to make a command that saves the roles of a user in the MongoDB. But now I'm struggling when the same user executes the command in an other server. How do I check form what server the command was executed and how do I save the roles per server
check = cursor.find({"guild_id": guild_id, "_id": self.ctx.author.id})
if check is None:
cursor.insert_one({"guild_id": guild_id, "_id": self.ctx.author.id, "name": str(user_name), "roles": [str(r.id) for r in self.ctx.author.roles[1:]]})
else:
cursor.update_one({"guild_id": guild_id, "_id": self.ctx.author.id}, {"$set": {"roles": [str(r.id) for r in self.ctx.author.roles[1:]]}})
I tried something like this, but I only get an error that the user already exists
Honestly: In many ways, it might be smarter not to store the role in a database, but instead rely on Discord, and look it up every time you need it, so using something like this for adding roles, for example:
async def change_role(
self,
*,
member: discord.Member,
role: discord.Role,
):
try:
if role in member.roles:
await member.remove_roles(role)
else:
await payload.member.add_roles(role)
except discord.Forbidden:
print("could not add role with current permissions") # Do with this what you want
However, if you insist on storing them, probably the best way is to use mongo for what it is good at, and store a json object. This is to say that you would create a json object for every server, which contains a list of members, each of which again contains it roles, this way, every member is stored per-server, and can therefore have different settings in each one. Right now, you're using Mongo like it's Postgres, and, in that case, it's probably better to use Postgres.
However, if you do want to do that. I'm expecting the error you're currently getting is because you have set the username field as a unique index in the header. You should remove that setting, as clearly, you now allow for multiple users to exist.
I hope that helps! :-)

Get instances during serializer validation in DRF

I am starting to work with the Django REST framework for a mini-reddit project I already developed.
The problem is that I am stuck in this situation:
A Minisub is like a subreddit. It has, among others, a field named managers which is ManyToMany with User.
An Ad is an advertising which will be displayed on the minisub, and it has a field named minisubs which is ManyToMany with Minisub. It has also a author field, foreign key with User.
I would like to allow these managers to add some ads on their minisubs through a DRF API. It is actually working. But I want to check that they put in minisubs only minisubs where they are managers.
I found a way like that:
class AdSerializer(serializers.HyperlinkedModelSerializer):
# ...
def validate_minisubs(self, value):
for m in value:
if user not in m.managers.all():
raise serializers.ValidationError("...")
return value
My question is: How to get user ? I can't find a way to get the value Ad.author (this field is set automatically in the serial data according to the user authentication). Maybe I don't find a way because there is no ways ? The place to do this is somewhere else ?
Thanks in advance.
You may get it out of the serializer this way:
class YourModelSeializer(serializers.HyperlinkedModelSerializer):
class Meta:
model=YourModel
def validate_myfield(self):
instance = getattr(self, 'instance', None)
...
I believe that this is a job for the permissions, if you are performing CRUD operations for inserting that into a database then u can have a permission class returns True if the user is a manager.
a permissions instance has access to the request which u can use to get the user and check if he is a manager:
http://www.django-rest-framework.org/api-guide/permissions/#custom-permissions

Building a database with Rails + Postgresql + Heroku

I'm building a web app where the user's need to choose from a database of exercises. They should be able to see the exercise name, description, and skill level. What is the best practice for building a database table for this purpose?
I'm thinking I could write each exercise and its attributes in a CSV file, then write a ruby script that would parse it and create an exercise object in the database table for each exercise that it parses. However, I have never done this before and would appreciate feedback.
And how would I migrate this database table from development to production on Heroku?
Thanks so much for any info.
Sounds like an Exercise model with name, description, skill level, and something to store the actual exercise. It is up to you to figure out whether you want to read it in with csv or yaml - I agree that yaml would be easier. Also how to store the user's responses - hstore, json, serialized yaml...
I ended up creating an array of hashes and iterating over the array in seeds.rb:
[
{
:name => "",
:description => "",
:skill_level => ""
},
# Input more hashes below
].each do |exercise|
ExerciseDb.create(
name: exercise[:name]
description: exercise[:description]
skill_level: exercise[:skill_level]
)
end

How do I create an admin in Ruby On Rails using the Devise Gem?

I am developing a rails application and am using a gem called devise to manage my users. I have created a new user called "Admin" but am unsure on how to change a user on the application from a "User" to an "Admin".
On the documentation it says:
"The code below can be used to grant admin status to the current user."
current_user.update_attribute :admin, true
But where would this snippet go?
Here is the documentation, The admin role creation info is near the bottom of the page.
https://github.com/plataformatec/devise/wiki/How-To%3A-Add-an-Admin-Role
You are very close to the solution! Nothing like reading through the documentation :-)
By going with Option 2 mentioned on the wikipage, users in your application will be classified
as 'regular' or 'admin', based on the admin attribute. The wikipage gives you the code for granting admin role to the current user, and leaves the decision of where to call this code up to you.
Fair enough, since how users become Admins is specific to each application, depending on how the users want it to be done.
One way to do it would be to have a 'Grant Current User Admin Rights' action in the GUI which would invoke the code. In that case, the code would go within a 'grant_current_user_admin_rights' method in the 'users_controller.rb' file. Of course, the views and the routes should be modified accordingly as well.
You could call that code from after_create callback on the user model, ensuring all users become Admins :-)
Another way to do it would be to set the admin flag for specific users either in the console or through database seeds.
Example from a seeds file on one of my projects:
admin_user = User.new( :email => USER_EMAIL, :password => PASSWORD_STRING, :name => USER_NAME )
admin_user.admin = true
admin_user.save!
Hope this helps.
4 years late but the true answer to OPs question, when he asked where to put:
current_user.update_attribute :admin, true
can be solved by going to the terminal/command prompt.
Type in
rails c
to access the rails terminal.
then go to your user, and since you're probably the first it'll be:
user = User.find(1)
user.update_attribute(:admin, true)
Assuming you followed all the previous steps in Option 2 of the documentation, this will set your user to have a true Admin attribute.
You can verify this by going
User.find(1)
and it should say "admin: true" at the end of the big block of text.

Get short user name from full name

Anyone know how to get a user's short user name, eg. "johnsmith", given their full name, eg. "John Smith"?
Note I'm interested in any user, not the current user, so functions like NSUserName are irrelevant.
Why? I am authenticating a username and password using Authorization Services. This allows people to enter either their short name or their full name, which is nice, but I then need to know who they've actually logged in as (ie. short user name and/or user id).
Nasty hacks like [NSHomeDirectoryForUser(username) lastPathComponent] don't work consistently.
You need to use the Collaboration Framework :).
Link this framework to your project, and then you just need to do the following:
CBIdentity* identity = [CBIdentity identityWithName:#"John Smith" authority:[CBIdentityAuthority localIdentityAuthority]];
NSLog(#"Posix name: %#", [identity posixName]);
And voilĂ !
EDIT: If you need to find only users that are bound on the network, you need to use +managedIdentityAuthority instead of +localIdentityAuthority.
And if you need to find both local users AND network users, use +defaultIdentityAuthority.

Resources