E Z H I L
E Z H I L10mo ago

Write TypeScript scripts interacting with Google Forms#9

For Google forms only with OAUTH 2.0 we can interact. Should I use https://www.npmjs.com/package/@googleapis/ ? @Hugo
42 Replies
Hugo
Hugo10mo ago
like explained in the readme, do not include the oauth logic in the script, this is handled by windmill the access token will be passed to the resource as the token property so you can directly query the google api with it
E Z H I L
E Z H I LOP10mo ago
Okay, then I should use token as env. windwill will handle oauth and return that token that is necessary. Correct me if I'm wrong
Hugo
Hugo10mo ago
it's right the only thing is you have to find a way to do the oauth flow locally to get a token to test your scripts
E Z H I L
E Z H I LOP10mo ago
Raised a PR. Can you review it please? @Hugo Can I change all the return to res.data to return the updated/created form
Hugo
Hugo10mo ago
yes
E Z H I L
E Z H I LOP10mo ago
And remove this part in all the tests?
No description
E Z H I L
E Z H I LOP10mo ago
Becuase forms is returned na
Hugo
Hugo10mo ago
response should be checked to match what you wanted to created (e.g. the form title / fields) and then you need to try to get the form and check that it exists
E Z H I L
E Z H I LOP10mo ago
Okay got it, will update the PR in sometime I am not sure what to do in setup.ts. Can you help me with that @Hugo Updated the rest of the files
Hugo
Hugo10mo ago
you should create the form you use in the tests
E Z H I L
E Z H I LOP10mo ago
import { beforeAll, afterAll } from 'bun:test'
import { resource } from './resource.ts'
import { google } from '@googleapis/forms'

// any sdk setup here if needed
const forms = google.forms({
version: 'v1',
auth: resource.token
})

beforeAll(async () => {
// create a form
const createForm = await forms.forms.create({
requestBody: {
info: {
title: 'Test Form'
}
}
})

console.log('BEFOREALL: Setup process')
})

afterAll(() => {
console.log('AFTERALL: Cleanup process')
})
import { beforeAll, afterAll } from 'bun:test'
import { resource } from './resource.ts'
import { google } from '@googleapis/forms'

// any sdk setup here if needed
const forms = google.forms({
version: 'v1',
auth: resource.token
})

beforeAll(async () => {
// create a form
const createForm = await forms.forms.create({
requestBody: {
info: {
title: 'Test Form'
}
}
})

console.log('BEFOREALL: Setup process')
})

afterAll(() => {
console.log('AFTERALL: Cleanup process')
})
Thats it in setup.ts right
Hugo
Hugo10mo ago
yes
E Z H I L
E Z H I LOP10mo ago
WIll remove the createForm const as well
Hugo
Hugo10mo ago
this will be run before all tests but you need to pass the form id to your tests probably by setting the environment variable in setup.ts as well in the tests you also have to query the api to check
E Z H I L
E Z H I LOP10mo ago
beforeAll(async () => {
// create a form
const createForm = await forms.forms.create({
requestBody: {
info: {
title: 'Test Form'
}
}
})

const formId = createForm.data.formId
beforeAll(async () => {
// create a form
const createForm = await forms.forms.create({
requestBody: {
info: {
title: 'Test Form'
}
}
})

const formId = createForm.data.formId
will this const formId be used everywhere in tests if I specify in this way?
Hugo
Hugo10mo ago
no you need to save it as an env variable
E Z H I L
E Z H I LOP10mo ago
the createForm has formId how to pass that to tests I think resourse.formId can be used to store the formId and reponseId what do you say? @Hugo
Hugo
Hugo10mo ago
no use environment variables => Bun.env.FORM_ID =
E Z H I L
E Z H I LOP10mo ago
Got it
Hugo
Hugo10mo ago
you can do inside setup.ts beforeAll
E Z H I L
E Z H I LOP10mo ago
Okay got it @Hugo Made all the changes. Let me know if I missed something
Hugo
Hugo10mo ago
i'll have a look, did you run all tests and checked that they pass?
E Z H I L
E Z H I LOP10mo ago
No, how can I get the token Most probably everything shoudl run correctly tested the apis externally and used the same conf
Hugo
Hugo10mo ago
Hey i've had a look: - the Create_Text_question must take a title, description and paragraph (bool) arguments as well - in Update_Form_Title you should check by a query to the api whether the form really has the new name - in List_form_responses you should check that the length of responses is 0 - test your work (you can use https://developers.google.com/oauthplayground/ to get a token) - fill in the readme with credentials instructions
OAuth 2.0 Playground
The OAuth 2.0 Playground lets you play with OAuth 2.0 and the APIs that supports it.
E Z H I L
E Z H I LOP10mo ago
Will update the PR and let you know in List_form_response when there is no response the api returns {} empty object
const forms = google.forms({
version: 'v1',
auth: resource.token
})
const forms = google.forms({
version: 'v1',
auth: resource.token
})
when access_token that is generated from playground is passed to auth. I'm getting Login Required error @Hugo
Hugo
Hugo10mo ago
did you grant the right permissions for the token?
E Z H I L
E Z H I LOP10mo ago
Yeah
E Z H I L
E Z H I LOP10mo ago
No description
E Z H I L
E Z H I LOP10mo ago
No description
E Z H I L
E Z H I LOP10mo ago
@Hugo Fixed the authentication part Tests are failing will fix them by EOD @Hugo FORM_ID can be taken from the object after a form is created. But without submitting response to the form. It is not possible to get the RESPONSE_ID. What could be done in this case? I have made some changes to the PR. Please check once It would be nice if we could skip tests that require RESPONSE_ID
Hugo
Hugo10mo ago
Just put instructions as comments inside the test
E Z H I L
E Z H I LOP10mo ago
How can I execute a test individually? Tests are executed in random order when FORM_ID and RESPONSE_ID are not even generated And fails
Hugo
Hugo10mo ago
i'm not sure i follow, but you should be able to setup what you need in setup.ts so that the tests work in any order or directly into each test file
E Z H I L
E Z H I LOP10mo ago
To be clear the problem is, only if create form is executed first we'll have FORM_ID for rest. Same applies for RESPONSE_ID after list response is executed. So test cases can't be passed due to this. Can I comment out assertion on all tests so that test case won't fail?
Hugo
Hugo10mo ago
that's why you should create the form in setup.ts or in each test create a form
E Z H I L
E Z H I LOP10mo ago
Okay I'll try either of these and get back with a result @Hugo Finally completed!
E Z H I L
E Z H I LOP10mo ago
E Z H I L
E Z H I LOP10mo ago
Pushed everything. Please review it once
Hugo
Hugo10mo ago
will have a look thanks
E Z H I L
E Z H I LOP10mo ago
@Hugo Have you checked my PR?
Hugo
Hugo10mo ago
done it's merged ! thanks for your contribution!
E Z H I L
E Z H I LOP10mo ago
Thanks for the opportunity. Btw it's a great project. Will continue contributing ✨

Did you find this page helpful?