/// import {WebApiClient,WebApiContext,WebApiConfiguration} from '../lib/core-sdk'; import {UsersResource} from './support/test-api/users'; import {PostsResource} from './support/test-api/posts'; import {useWebApiKey,useEnvironment,Environment,Judge,sharedContext,config,JudgeSession,PaginatedListResponse, TestUtils} from '../lib/core-sdk'; import {CreateUserRequest,UpdateUserRequest,User} from './support/test-api/users'; import {Post} from './support/test-api/posts'; export class TestApiClient extends WebApiClient { constructor(config: WebApiConfiguration){ super(config,""); } get users():UsersResource{ return new UsersResource(this.getPath()+"/users",this); } get posts():PostsResource{ return new PostsResource(this.getPath()+"/posts",this); } } describe('Access token',function(){ var client : TestApiClient var judge : Judge var judgeSession : JudgeSession var originalTimeoutInterval = null; beforeAll(function(){ //Because Judge starts slowly on the first request originalTimeoutInterval = jasmine.DEFAULT_TIMEOUT_INTERVAL; jasmine.DEFAULT_TIMEOUT_INTERVAL = 30000; }); afterAll(function(){ jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeoutInterval; }); beforeEach(function(){ useWebApiKey("TEST_API_KEY").useEnvironment(new Environment("http://csas-judge.herokuapp.com","http://csas-judge.herokuapp.com/widp/oauth2")).useOauth2({ clientId: 'TestClientID', redirectUri: 'csastest://auth-completed', clientSecret: 'TestClientSecret' }); judge = new Judge(); judgeSession = judge.startNewSession(); client = new TestApiClient(config); client.sharedContext = sharedContext; client.sharedContext.useCode('TestCode'); client.sharedContext.accessTokenProvider.useAccessToken({ refreshToken: "TestRefreshToken", token : "TestAccessToken", expiresIn: 3600, tokenType: 'bearer' }); }); it('calls api with access token', done => { judgeSession.setNextCase('accessTokenProvider.refresh.succesful.sanitized').then(() => { return client.users.list({ pageNumber: null, pageSize: null }).then(response => { expect(response.items[0].userId).toBe(3); done(); }).catch(e => { console.log(e) console.log(e.response.data.errors); }); }); }); it('calls api with access token and fails with 400 bad request', done => { judgeSession.setNextCase('accessTokenProvider.refresh.badRequest.sanitized').then(() => { return client.users.list({ pageNumber: null, pageSize: null }).catch(e => { expect(e.statusCode).toBe(400); done(); }); }); }); it('calls api with access token and fails with 403 forbidden', done => { judgeSession.setNextCase('accessTokenProvider.refresh.forbidden.sanitized').then(() => { return client.users.list({ pageNumber: null, pageSize: null }).catch(e => { expect(e.statusCode).toBe(403); done(); }); }); }); it('tests processRedirect on AccessTokenProvider', () => { let token = client.sharedContext.processRedirect('http://somesite.com/some/path#/code=asdjakdalsjdalS9809SDHD09809'); expect(token).toBeTruthy(); }); }); describe('Access token implicit flow',function(){ var client : TestApiClient var judge : Judge var judgeSession : JudgeSession var originalTimeoutInterval = null; beforeAll(function(){ //Because Judge starts slowly on the first request originalTimeoutInterval = jasmine.DEFAULT_TIMEOUT_INTERVAL; jasmine.DEFAULT_TIMEOUT_INTERVAL = 30000; }); afterAll(function(){ jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeoutInterval; }); beforeEach(function(){ useWebApiKey("TEST_API_KEY").useEnvironment(new Environment("http://csas-judge.herokuapp.com","http://csas-judge.herokuapp.com/widp/oauth2")).useOauth2({ clientId: 'TestClientID', redirectUri: 'csastest://auth-completed', }); judge = new Judge(); judgeSession = judge.startNewSession(); client = new TestApiClient(config); client.sharedContext = sharedContext; }); it('tests processRedirect on SharedContext', () => { let token = client.sharedContext.processRedirect('http://somesite.com/some/path#/access_token=ASDPOWQI80808ADASD&expires_in=3600&token_type=bearer&lang=cs'); let token2 = client.sharedContext.processRedirect('http://somesite.com/some/path#/expires_in=3600&access_token=ASDPOWQI80808ADASD&token_type=bearer'); let token3 = client.sharedContext.processRedirect('http://somesite.com/some/path#/token_type=bearer&expires_in=3600&access_token=ASDPOWQI80808ADASD'); let token4 = client.sharedContext.processRedirect('http://somesite.com/some/path#/scope=profile&token_type=bearer&expires_in=3600&access_token=ASDPOWQI80808ADASD&lang=cs'); token .then(token => { TestUtils.expectToBe(token, { token: 'ASDPOWQI80808ADASD', tokenType: 'bearer', }); expect(token.expiresIn).toBeTruthy(); }); token2 .then(token2 => { TestUtils.expectToBe(token2, { token: 'ASDPOWQI80808ADASD', tokenType: 'bearer', }); expect(token2.expiresIn).toBeTruthy(); }); token3 .then(token3 => { TestUtils.expectToBe(token3, { token: 'ASDPOWQI80808ADASD', tokenType: 'bearer', }); expect(token3.expiresIn).toBeTruthy(); }); token4 .then(token4 => { TestUtils.expectToBe(token4, { token: 'ASDPOWQI80808ADASD', tokenType: 'bearer', }); expect(token4.expiresIn).toBeTruthy(); }); }); });