Custom response and parameters for baucis swagger docs - baucis

I see that it is relatively easy to add docs to swagger UI for custom endpoints in baucis.
var controller = baucis.rest('User');
controller.swagger.apis.push({
'path': '/users/login',
'description': 'Logs a user in',
'operations': [
{
'httpMethod': 'POST',
'nickname': 'Login',
'responseClass': 'User',
'summary': 'Requests auth token given username and password'
}]})
However, in this case above, I'd like to set the response class to {token : string} instead of 'User', and set parameters to {username: string, password: string}, so that the 'Try it out!' button works.
Any ideas how I would go about this? Or if it is possible in baucis-swagger?

Seems the baucis object above just follows the swagger specification.
Adding a parameters definitions object array, and a responses definition object array to the object above solves my problem.

Related

How to mock or spy on recaptcha in Jasmine

I added recaptcha-v2 in my Angular6 forms. How do I unit-test the form? I want to test that the form under test is invalid if recaptcha isn't clicked and also the form gets submitted if recaptcha is clicked.
The test I have written so far is
fit('it should emit form values if both username and password fields in the login form are correctly filled', () => {
const loginComponent = component;
spyOn(loginComponent.formOutputEvent, 'emit');
const formControls = loginComponent.loginForm.controls;
const email = 'test#test.com';
const password = 'testpassword';
formControls['userName'].setValue(email);
formControls['password'].setValue(password);
formControls['recaptcha'].setValue({}); //what value to set here?
loginComponent.loginFormSubmitted();
expect(loginComponent.formOutputEvent.emit).toHaveBeenCalledWith(new LoginFormValues(email, password));
});
I am getting error Expected spy emit to have been called with [ LoginFormValues({ username: 'test#test.com', password: 'testpassword', recaptcha: '' }) ] but actual calls were [ LoginFormValues({ username: 'test#test.com', password: 'testpassword', recaptcha: Object({ }) }) ].
Interesting, the following works even though LoginFormValues expects the 3rd argument to be of type string but {} isn't
expect(loginComponent.formOutputEvent.emit).toHaveBeenCalledWith(new LoginFormValues(email, password,{}));
But this fails because {}.toString(). gets converted to [Object object].
expect(loginComponent.formOutputEvent.emit).toHaveBeenCalledWith(new LoginFormValues(email, password,{}.toString));
I am still waiting for better answer

stubbing responses with Cypress

I am trying to stub a response of GET request which basically returns an array that is filled in the dropdown menu
I tried this code
// stubbing get a list of schedulers
cy.server()
cy.route({
method: 'GET',
url: '/companies/cccccccc-1111-1111-1111-111111111111/attendance/shift-scheduler/schedulers', // company one
JSON: true,
Response: [
{
id: "eeee3333-1111-1111-1111-111111111111",
first_name: "Admin One",
}
]
})
But the list still not filled even though I am not receiving any error, however, I cannot see the stubbed GET reqguest in the UI of cypress. Did I do something wrong?

Passing a token through Query?

I have a Graph QL server running (Apollo Server 2) and the API behind it requires every request to include a token.
Currently the token comes from HTTP Request Cookie. This was simple enough to work. When the request comes in, grab the cookie from the header and pass it along to the HTTP request to be sent to the API server through the resolvers.
I'd like to make it so a GraphQL client can pass this token along through the POST query itself.
Basically wondering if I can define a global GQL variable of some sort. "All queries, this variable is required."
I had a similar implementation in Typescript, and in order to achieve something like this, I've define an object:
const globalInput = {
token: {
type: GraphQLString;
}
}
And then use it in your GraphQLObjectType:
const Query = new GraphQLObjectType({
name: 'Query',
fields: () => ({
myObject: {
type: MyTypeObject,
args: { ...globalInput },
resolve: (source: any, args: any) => {
// global input values can be access in args
// ex: args.token
return {}
}
}
})
})
The problem is that I need to extend it(...globalInput) it in every object type.
But it does the job.

How to use rxjs ajax operator instead of axios in my react project?

I am new to rxjs and want to know how to handle this use case.
This is axios promise, how to convert it so that it uses rxjs's ajax operator
export const onLogin = ({ username, password }) =>
axios({
method: "post",
url: O_TOKEN_URL,
data: querystring.stringify({
client_id: CLIENT_ID,
client_secret: CLIENT_SECRET,
grant_type: "password",
username,
password
}),
headers: {
"Content-Type": "application/x-www-form-urlencoded"
}
});
This is my action,
export const onSubmit = payload => ({
type: FETCH_USER,
payload // payload contains username & password
});
This is my epic for now,
export const loginEpic = action$ =>
action$
.ofType(FETCH_USER)
// somehow import onLogin from above and resolve it
// then, dispatch FETCH_USER_FULFILLED
.do(
payload => console.log(payload.username, payload.password)
// i am able to console these username and password
)
.mapTo(() => null);
I want to resolve onLogin function somehow, when FETCH_USER is dispatched, using rxjs's ajax operator.
And, I want onLogin function, which returns promise/observable, to be set up in different file so that I can keep track of all the ajax requests
These are the packages,
"redux-observable": "^0.18.0",
"rxjs": "^5.5.10",
Could you also point me to a documentation that covers this and various use case for post, put ... requests? I couldn't find any.
The ajax config object is fairly similar to what you already have. I'm assuming the data property for the axios request is the request body.
import {ajax} from 'rxjs/observable/dom/ajax';
export const onLogin = ({ username, password }) =>
ajax({
method: "POST",
url: O_TOKEN_URL,
body: JSON.stringify({
client_id: CLIENT_ID,
client_secret: CLIENT_SECRET,
grant_type: "password",
username,
password
}),
headers: {
"Content-Type": "application/x-www-form-urlencoded"
}
});
Your epic would look something like this:
export const loginEpic = action$ =>
action$
.ofType(FETCH_USER)
.mergeMap(action =>
onLogin(action.payload)
// map will be called when the request succeeded
// so we dispatch the success action here.
.map((ajaxResponse) => fetchUserFulfilled())
// catch will be called if the request failed
// so we dispatch the error action here.
// Note that you must return an observable from this function.
// For more advanced cases, you can also apply retry logic here.
.catch((ajaxError, source$) => Observable.of(fetchUserFailed()))
);
Where fetchUserFulfilled and fetchUserFailed are action creator functions.
There does not seem to be much documentation of the RxJS 5 ajax method yet. Here are the links to the official v5 docs for the AjaxRequest, AjaxResponse and AjaxError. The AjaxError object in particular has 0 information so far (at the time of this answer) so you will need to rely on the source code if you need to use this object for more than a trigger to tell the user that something went wrong. The ajax source code is here.

Override forms in django allauth

I'm building a project in Django, and I'm using django-allauth for the social authentication, but I had already set up an authentication system of my own.
Now, as I said, I just wanted to use django-allauth for the social authentication, but NOT for the classic authentication, since - I have tried it - it's authentication system collides with my own.
I've tried overriding the form by creating a FormView like the following:
class LoginUser(FormView):
template_name = 'account/login.html'
form_class = MyLoginForm
That calls this form:
class MyLoginForm(forms.ModelForm):
"""
A form that login a user.
"""
email = forms.EmailField(label='', required=True, widget = forms.TextInput(
attrs = {
'placeholder': 'DIOCAAAAA',
'class': 'form-control',
'id': 'login-email'
}
))
password1 = forms.CharField(label='', required=True, widget=forms.PasswordInput(attrs = {
'placeholder': 'Password',
'class': 'form-control',
'id': 'login-password',
'data-parsley-trigger': 'focusout'
}
))
class Meta:
model = CustomUser
fields = ('email',)
And I've added this in the urls:
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^accounts/', include('allauth.urls')),
**url(r'^accounts/login/$', views.LoginUser.as_view()),**
[...]
)
But I get this error:
TypeError at /accounts/login/
init() got an unexpected keyword argument 'request'
Am I proceeding in the right direction, or should I try something else?
Try to change your Url to be different location,I think your url conflict with allauth url,because allauth has already had this
url(r'^accounts/login/$')

Resources