<commit_before>package models.organization

import play.api.db.slick.Config.driver.simple._
import org.specs2.mutable._
import org.specs2.runner._
import org.junit.runner._
import play.api.test._
import play.api.test.Helpers._
import play.api.db.slick.DB
import scala.slick.session.Session
import models.DBTest.inMemH2
import play.api.test.FakeApplication
import models.EquationsModel
import models.DBTest
import service.UserTmpTest
import models.organization._
import models.organization.table._
import service._
import service.table._

// TODO check out http://workwithplay.com/blog/2013/06/19/integration-testing/
@RunWith(classOf[JUnitRunner])
class CoursesSpec extends Specification {

	"grantAccess" should {
		
		"grant the requested access" in new WithApplication(FakeApplication(additionalConfiguration = inMemH2)) {
			DB.withSession { implicit session: Session =>
				val course = Courses.create(CoursesTmpTest(owner = DBTest.fakeUser(UserTmpTest()).id))
				val user = DBTest.fakeUser(UserTmpTest())
				
				course.grantAccess(Edit)(user, session)
				
				course.access(user, session) must beEqualTo(Edit)
			}
		}
		
		"keep the higher access if access is granted twice" in new WithApplication(FakeApplication(additionalConfiguration = inMemH2)) {
			DB.withSession { implicit session: Session =>
				val course = Courses.create(CoursesTmpTest(owner = DBTest.fakeUser(UserTmpTest()).id))
				val user = DBTest.fakeUser(UserTmpTest())
				
				course.grantAccess(Edit)(user, session)
				course.grantAccess(View)(user, session)
				
				course.access(user, session) must beEqualTo(Edit)
			}
		}
		
		"only grant access to the requested course" in new WithApplication(FakeApplication(additionalConfiguration = inMemH2)) {
			DB.withSession { implicit session: Session =>
				val course1 = Courses.create(CoursesTmpTest(owner = DBTest.fakeUser(UserTmpTest()).id))
				val course2 = Courses.create(CoursesTmpTest(owner = DBTest.fakeUser(UserTmpTest()).id))
				val course3 = Courses.create(CoursesTmpTest(owner = DBTest.fakeUser(UserTmpTest()).id))
				val user = DBTest.fakeUser(UserTmpTest())
				
				course1.grantAccess(Edit)(user, session)
				course2.grantAccess(View)(user, session)
				
				course1.access(user, session) must beEqualTo(Edit)
				course2.access(user, session) must beEqualTo(View)
				course3.access(user, session) must beEqualTo(Non)
			}
		}
	}
	
	"listDetails: access to course" should {

		"be Non when a user has no connection to the course" in new WithApplication(FakeApplication(additionalConfiguration = inMemH2)) {
			DB.withSession { implicit session: Session =>
				val owner = DBTest.fakeUser(UserTmpTest())
				val accessor = DBTest.fakeUser(UserTmpTest())
				val course = Courses.create(CoursesTmpTest(owner = owner.id))
				val courseDetails = Courses.listDetails(accessor, session)(0)

				courseDetails.a must beEqualTo(Non)
			}
		}

		"be Own when a user is the owner of the course" in new WithApplication(FakeApplication(additionalConfiguration = inMemH2)) {
			DB.withSession { implicit session: Session =>
				val owner = DBTest.fakeUser(UserTmpTest())
				val course = Courses.create(CoursesTmpTest(owner = owner.id))
				val courseDetails = Courses.listDetails(owner, session)(0)

				courseDetails.a must beEqualTo(Own)
			}<commit_msg>rounding out corner case in course access<commit_after>package models.organization

import play.api.db.slick.Config.driver.simple._
import org.specs2.mutable._
import org.specs2.runner._
import org.junit.runner._
import play.api.test._
import play.api.test.Helpers._
import play.api.db.slick.DB
import scala.slick.session.Session
import models.DBTest.inMemH2
import play.api.test.FakeApplication
import models.EquationsModel
import models.DBTest
import service.UserTmpTest
import models.organization._
import models.organization.table._
import service._
import service.table._

// TODO check out http://workwithplay.com/blog/2013/06/19/integration-testing/
@RunWith(classOf[JUnitRunner])
class CoursesSpec extends Specification {

	"grantAccess" should {
		
		"grant the requested access" in new WithApplication(FakeApplication(additionalConfiguration = inMemH2)) {
			DB.withSession { implicit session: Session =>
				val course = Courses.create(CoursesTmpTest(owner = DBTest.fakeUser(UserTmpTest()).id))
				val user = DBTest.fakeUser(UserTmpTest())
				
				course.grantAccess(Edit)(user, session)
				
				course.access(user, session) must beEqualTo(Edit)
			}
		}
		
		"keep the higher access if access is granted twice" in new WithApplication(FakeApplication(additionalConfiguration = inMemH2)) {
			DB.withSession { implicit session: Session =>
				val course = Courses.create(CoursesTmpTest(owner = DBTest.fakeUser(UserTmpTest()).id))
				val user = DBTest.fakeUser(UserTmpTest())
				
				course.grantAccess(Edit)(user, session)
				course.grantAccess(View)(user, session)
				
				course.access(user, session) must beEqualTo(Edit)
			}
		}
		
		"only grant access to the requested course" in new WithApplication(FakeApplication(additionalConfiguration = inMemH2)) {
			DB.withSession { implicit session: Session =>
				val course1 = Courses.create(CoursesTmpTest(owner = DBTest.fakeUser(UserTmpTest()).id))
				val course2 = Courses.create(CoursesTmpTest(owner = DBTest.fakeUser(UserTmpTest()).id))
				val course3 = Courses.create(CoursesTmpTest(owner = DBTest.fakeUser(UserTmpTest()).id))
				val user = DBTest.fakeUser(UserTmpTest())
				
				course1.grantAccess(Edit)(user, session)
				course2.grantAccess(View)(user, session)
				
				course1.access(user, session) must beEqualTo(Edit)
				course2.access(user, session) must beEqualTo(View)
				course3.access(user, session) must beEqualTo(Non)
			}
		}
		
		"only grant the spcified user access to the requested course" in new WithApplication(FakeApplication(additionalConfiguration = inMemH2)) {
			DB.withSession { implicit session: Session =>
				val course = Courses.create(CoursesTmpTest(owner = DBTest.fakeUser(UserTmpTest()).id))
				val userWithAccess = DBTest.fakeUser(UserTmpTest())
				val userNoAccess = DBTest.fakeUser(UserTmpTest())
				
				course.grantAccess(Edit)(userWithAccess, session)
				
				course.access(userWithAccess, session) must beEqualTo(Edit)
				course.access(userNoAccess, session) must beEqualTo(Non)
			}
		}
	}
	
	"listDetails: access to course" should {

		"be Non when a user has no connection to the course" in new WithApplication(FakeApplication(additionalConfiguration = inMemH2)) {
			DB.withSession { implicit session: Session =>
				val owner = DBTest.fakeUser(UserTmpTest())
				val accessor = DBTest.fakeUser(UserTmpTest())
				val course = Courses.create(CoursesTmpTest(owner = owner.id))
				val courseDetails = Courses.listDetails(accessor, session)(0)

				courseDetails.a must beEqualTo(Non)
			}
		}

		"be Own when a user is the owner of the course" in new WithApplication(FakeApplication(additionalConfiguration = inMemH2)) {
			DB.withSession { implicit session: Session =>
				val owner = DBTest.fakeUser(UserTmpTest())
				val course = Courses.create(CoursesTmpTest(owner = owner.id))
				val courseDetails = Courses.listDetails(owner, session)(0)

				courseDetails.a must beEqualTo(Own)
			}
{'fraction_non_alphanumeric': 0.104490022172949, 'fraction_numerical': 0.007344789356984479, 'mean_word_length': 4.598913886733902, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5990066', 'n_tokens_mistral': 2528, 'n_tokens_neox': 2167, 'n_words': 492}